#include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; const int MAXN = 100 * 1000 + 10; vector<int> g[MAXN]; vector<int> gRev[MAXN]; vector<int> * comp; vector<int> comps[MAXN]; int cntComps; int was[MAXN]; int order[MAXN]; int topSortPtr; unordered_set<int> G[MAXN]; int inComp[MAXN]; vector<int> ans; vector<int> compNeed; int curNeed; int vis[MAXN]; void topSort(int v) { was[v] = 1; for (int u : g[v]) { if (!was[u]) { topSort(u); } } order[topSortPtr++] = v; } void dfs(int v) { was[v] = true; inComp[v] = cntComps; if (vis[v]) comp->push_back(v); for (int u : gRev[v]) { if (!was[u]) { dfs(u); } } } bool go(int v) { if (was[v]) return false; bool need = false; if (compNeed.size() <= curNeed) return false; int nowNeed = compNeed[curNeed]; if (nowNeed < v) { return false; } else if (nowNeed == v) { need = true; curNeed++; } was[v] = true; for (int vv : comps[v]) { if (vis[vv]) { ans.push_back(vv); } } for (int u : G[v]) { if (go(u)) { return true; } } return need; } void solve() { cntComps = 0; topSortPtr = 0; ans = vector<int>(); compNeed = vector<int>(); int n, m, k; cin >> n >> m >> k; fill(vis, vis + n, 0); for (int i = 0; i < k; i++) { int tt; scanf("%d", &tt); vis[tt - 1] = 1; } fill(inComp, inComp + n, 0); for (int i = 0; i < n; i++) { g[i] = vector<int>(); gRev[i] = vector<int>(); } for (int i = 0; i < m; i++) { int a = 0, b = 0; scanf("%d%d", &a, &b); a--; b--; g[a].push_back(b); gRev[b].push_back(a); } fill(was, was + n, 0); fill(order, order + n, 0); for (int i = 0; i < n; i++) { if (!was[i]) { topSort(i); } } fill(was, was + n, 0); for (int i = n - 1; i >= 0; i--) { int v = order[i]; if (!was[v]) { comp = &comps[cntComps]; comps[cntComps].clear(); dfs(v); sort(comp->begin(), comp->end()); for (int x : *comp) { if (vis[x]) { compNeed.push_back(cntComps); break; } } cntComps++; } } for (int i = 0; i < n; i++) { G[i] = unordered_set<int>(); } for (int v = 0; v < n; v++) { for (int u : g[v]) { G[inComp[v]].insert(inComp[u]); } } /* printf("========= %d\n", cntComps); for (int i = 0; i < cntComps; i++) { if (comps[i].size() == 0) continue; for (int x : comps[i]) { printf("%d ", x + 1); } printf("\n"); } printf("==================");*/ fill(was, was + cntComps, 0); for (int i = n - 1; i >= 0; i--) { int v = order[i]; if (vis[v]) { curNeed = 0; go(inComp[v]); if (ans.size() != k) { cout << -1 << endl; } else { for (int x : ans) { printf("%d ", x + 1); } printf("\n"); } return; } } } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } }