#include <bits/stdc++.h> using namespace std; struct StronglyConnectedComponents { int n; int totalComponents; vector <vector <int>> adj, components; vector <int> idx, componentOf, st, low; vector <bool> inStack; StronglyConnectedComponents (int n): n(n), adj(n) {} void AddEdge (int a, int b) { adj[a].push_back(b); } int DFS (int v, int index) { idx[v] = index; low[v] = index; index += 1; st.push_back(v); inStack[v] = true; for (auto& w : adj[v]) { if (idx[w] == -1) { index = DFS(w, index); low[v] = min(low[v], low[w]); } else if (inStack[w]) { low[v] = min(low[v], low[w]); } } if (low[v] == idx[v]) { int w; components.push_back(vector <int>()); do { w = st.back(); st.pop_back(); inStack[w] = false; componentOf[w] = totalComponents; components[totalComponents].push_back(w); } while (w != v); totalComponents++; sort(components.back().begin(), components.back().end(), greater<int>()); } return index; } void BuildSCC () { totalComponents = 0; idx = vector <int>(n, -1), low = vector <int>(n), componentOf = vector <int>(n); inStack = vector <bool>(n, false); st.clear(); for (int i = 0; i < n; i++) { if (idx[i] == -1) { DFS(i, 0); } } } bool isReachable(int a, int b, vector <bool>& visited) { if (componentOf[a] == componentOf[b]) { return true; } queue <int> Q; Q.push(a); visited[a] = true; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int v : adj[u]) { if (componentOf[v] == componentOf[b]) { return true; } else if (componentOf[v] > componentOf[b] && !visited[v]) { Q.push(v); visited[v] = true; } } } return false; } bool PrintSolution (const set <int>& to_visit) { BuildSCC(); int prev = -1; vector <int> solution; vector <bool> visited(n, false); for (auto& comp : components) { for (int v : comp) { if (to_visit.find(v) != to_visit.end()) { if (prev != -1 && !isReachable(v, prev, visited)) { return false; } solution.push_back(v + 1); prev = v; } } } reverse(solution.begin(), solution.end()); cout << solution[0]; for (int i = 1; i < solution.size(); i++) { cout << ' ' << solution[i]; } cout << endl; return true; } }; int main(int argc, char const * argv[]) { ios::sync_with_stdio(false); int t; cin >> t; for (int cs = 0; cs < t; cs++) { int n, m, k; cin >> n >> m >> k; set <int> to_visit; for (int i = 0; i < k; i++) { int v; cin >> v; to_visit.insert(v - 1); } StronglyConnectedComponents G(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; G.AddEdge(a - 1, b - 1); } if (!G.PrintSolution(to_visit)) { cout << -1 << endl; } } return 0; }