#include <cassert> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; int T; int N, M, K; bool is[100002]; vector<int> V[100002], W[100002], comps[100002]; int where[100002], D[100002], minD[100002]; int ST[100002], C[100002], E[100002]; bool inST[100002], S[100002], done[100002]; queue<int> Q; void Tarjan(int x) { D[x] = ++D[0]; minD[x] = D[x]; ST[++ST[0]] = x; for (auto nxt : V[x]) if (!D[nxt]) { Tarjan(nxt); minD[x] = min(minD[x], minD[nxt]); } else if (!where[nxt]) minD[x] = min(minD[x], minD[nxt]); if (D[x] == minD[x]) { ++where[0]; while (true) { where[ST[ST[0]]] = where[0]; if (is[ST[ST[0]]]) comps[where[0]].push_back(ST[ST[0]]); --ST[0]; if (x == ST[ST[0] + 1]) break; } } } void Dfs(int x) { inST[x] = true; for (auto nxt : V[x]) if (!inST[nxt]) Dfs(nxt); ST[++ST[0]] = x; } bool findPath(int x, int y) { if (S[x]) return false; S[x] = true; bool found = false; Q.push(x); while (!Q.empty()) { int now = Q.front(); Q.pop(); for (auto nod : W[now]) { if (!S[nod]) { S[nod] = true; Q.push(nod); } if (nod == y) found = true; } } return found; } int main() { cin >> T; while (T--) { for (int i = 1; i <= 100000; ++i) { V[i].clear(); W[i].clear(); comps[i].clear(); } memset(is, 0, sizeof(is)); memset(where, 0, sizeof(where)); memset(inST, 0, sizeof(inST)); memset(D, 0, sizeof(D)); memset(minD, 0, sizeof(minD)); ST[0] = 0; cin >> N >> M >> K; for (int i = 1, nod; i <= K; ++i) { cin >> nod; is[nod] = true; } for (int i = 1, nod1, nod2; i <= M; ++i) { cin >> nod1 >> nod2; V[nod1].push_back(nod2); } for (int i = 1; i <= N; ++i) if (!D[i]) Tarjan(i); for (int i = 1; i <= where[0]; ++i) sort(comps[i].begin(), comps[i].end()); for (int i = 1; i <= N; ++i) for (auto nxt : V[i]) if (where[i] != where[nxt]) W[where[i]].push_back(where[nxt]); ST[0] = 0; for (int i = 1; i <= N; ++i) if (!inST[i]) Dfs(i); // there can only be one path => get them in order memset(done, 0, sizeof(done)); E[0] = 0; for (int i = ST[0]; i >= 1; --i) if (is[ST[i]] && !done[where[ST[i]]]) { E[++E[0]] = where[ST[i]]; done[where[ST[i]]] = true; } C[0] = 0; for (int i = 1; i <= E[0]; ++i) for (auto nod : comps[E[i]]) C[++C[0]] = nod; // verify bool ok = true; memset(S, 0, sizeof(S)); for (int i = E[0] - 1; i >= 1; --i) ok &= findPath(E[i], E[i + 1]); if (!ok) cout << -1 << '\n'; else { for (int i = 1; i <= C[0]; ++i) cout << C[i] << ' '; cout << '\n'; } } }