#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; // Do NOT forget to initialize all! int N, M, K; bool hasToVisit[MAX_N]; vector<int> g[MAX_N], gRev[MAX_N]; int curTime; int visitTime[MAX_N]; void calcTimes(int ind) { if (visitTime[ind]) return; visitTime[ind] = -1; for (int i = 0; i < gRev[ind].size(); ++i) calcTimes(gRev[ind][i]); visitTime[ind] = curTime++; } // Building SCC Stuff vector<int> scc[MAX_N]; int sccNodeItBelongsTo[MAX_N]; set<int> specialNodes[MAX_N]; int curNode; void buildSCC(int ind) { if (sccNodeItBelongsTo[ind] != -1) return; sccNodeItBelongsTo[ind] = curNode; if (hasToVisit[ind]) specialNodes[curNode].insert(ind); for (int i = 0; i < g[ind].size(); ++i) if (sccNodeItBelongsTo[g[ind][i]] == -1) buildSCC(g[ind][i]); else if (sccNodeItBelongsTo[g[ind][i]] != curNode) scc[curNode].push_back(sccNodeItBelongsTo[g[ind][i]]); } int dp[MAX_N]; int solve(int ind) { int &ret = dp[ind]; if (ret != -1) return ret; ret = 0; for (int i = 0; i < scc[ind].size(); ++i) ret = max(ret, solve(scc[ind][i])); ret += specialNodes[ind].size(); return ret; } vector<int> path; void buildPath(int ind) { for (auto s : specialNodes[ind]) path.push_back(s); for (int i = 0; i < scc[ind].size(); ++i) if (solve(scc[ind][i]) + specialNodes[ind].size() == solve(ind)) { buildPath(scc[ind][i]); return; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while(T--) { cin >> N >> M >> K; memset(hasToVisit, 0, sizeof(hasToVisit)); for (int i = 0; i < N; ++i) g[i].clear(), gRev[i].clear(); int x, y; for (int i = 0; i < K; ++i) cin >> x, hasToVisit[x - 1] = true; for (int i = 0; i < M; ++i) cin >> x >> y, --x, --y, g[x].push_back(y), gRev[y].push_back(x); memset(visitTime, 0, sizeof(visitTime)); curTime = 1; // End of initialization // determine sink nodes, which are source nodes of gRev vector<pair<int, int> > nodesToVisit(N); for (int i = 0; i < N; ++i) calcTimes(i), nodesToVisit[i] = make_pair(visitTime[i], i); // Times checking //for (int i = 0; i < N; ++i) // cout << i << ", " << visitTime[i] << endl; sort(nodesToVisit.rbegin(), nodesToVisit.rend()); for (int i = 0; i < N; ++i) scc[i].clear(), sccNodeItBelongsTo[i] = -1, specialNodes[i].clear(); curNode = 0; for (int i = 0; i < N; ++i) if (sccNodeItBelongsTo[nodesToVisit[i].second] == -1) { //cout << nodesToVisit[i].second << endl; buildSCC(nodesToVisit[i].second); curNode++; } // SCC Checking //for (int i = 0; i < curNode; ++i) { // cout << "Special Nodes:"; // for (auto s : specialNodes[i]) // cout << " " << s; // cout << endl; // cout << "Adjacent Nodes:"; // for (int j = 0; j < scc[i].size(); ++j) // cout << " " << scc[i][j]; // cout << endl; //} memset(dp, -1, sizeof(dp)); for (int i = 0; i < curNode; ++i) { if (solve(i) == K) { path.clear(); buildPath(i); for (int i = 0; i < path.size(); ++i) cout << path[i] + 1 << " "; cout << "\n"; goto END; } } cout << "-1\n"; END:; } return 0; }