//by Tanmay Chaudhari #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif //#pragma comment(linker, "/STACK:66777216") #include <bits/stdc++.h> using namespace std; #define si(a) scanf("%d",&a) #define sl(a) scanf("%lld",&a) #define pi(a) printf("%d\n",a) #define pl(a) printf("%lld\n",a) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<vi> vvi; typedef vector<ii> vii; #define SET(a,b) memset(a,b,sizeof(a)) #define forall(i,a,b) for(int i=a; i<b; i++) #define forrev(i,a,b) for(int i=a; i>=b; i--) #define forr(it,container) for(auto it=container.begin(); it!=container.end(); it++) #define w(t) int t;si(t);while(t--) #define TRACE #ifdef TRACE #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl; #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl; #else #define trace1(x) #define trace2(x, y) #define trace3(x, y, z) #define trace4(a, b, c, d) #define trace5(a, b, c, d, e) #define trace6(a, b, c, d, e, f) #endif int arr[1 << 20], cnter, par[1<<20]; bool tobevisited[1 << 20]; bool vis[1 << 20], vis2[1<<20], vis3[1<<20], vis4[1<<20], mark[1<<20]; stack<int > stk1, stk2; vector<int > graph[1 << 20], revgraph[1 << 20], scc[1 << 20], newgrph[1<<20]; vi v; set<pair<int, int > > components; void dfs0(int u) { vis[u] = 1; forall(i, 0, graph[u].size()) if (!vis[graph[u][i]]) dfs0(graph[u][i]); stk1.push(u); } void dfs1(int u) { vis2[u] = 1; scc[cnter].push_back(u); if (tobevisited[u]) mark[cnter] = 1; par[u] = cnter; forall(i, 0, revgraph[u].size()) { int to = revgraph[u][i]; if (!vis2[to]) dfs1(to); } } void dfs2(int u) { vis3[u] = 1; forall(i, 0, newgrph[u].size()) { int to = newgrph[u][i]; if (!vis[to]) dfs2(to); } stk2.push(u); } void dfs3(int u, int p) { if (u != p) vis4[u] = 1; forall(i, 0, newgrph[u].size()) { int to = newgrph[u][i]; if (!vis4[to]) dfs3(to, p); } } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); w(t) { int k, n, m; cnter = 1; si(n); si(m); si(k); forall(i, 0, k) { si(arr[i]); tobevisited[arr[i]] = 1; } forall(i, 0, m) { int x, y; si(x); si(y); graph[x].push_back(y); revgraph[y].push_back(x); } forall(i, 1, n + 1) if (!vis[i]) dfs0(i); while (!stk1.empty()) { int top = stk1.top(); stk1.pop(); if (!vis2[top]) { mark[cnter] = 0; dfs1(top); cnter++; } } forall(i, 1, n+1) forall(j, 0, graph[i].size()) if (par[i] != par[graph[i][j]]) components.insert(make_pair(par[i], par[graph[i][j]])); for (auto i = components.begin(); i != components.end(); i++) newgrph[(*i).first].push_back((*i).second); forall(i, 1, cnter) if (!vis3[cnter]) dfs2(i); while (!stk2.empty()) { int top = stk2.top(); stk2.pop(); if (mark[top]) v.push_back(top); } bool notvalid = 0; forall(i, 1, v.size()) { dfs3(v[i], v[i]); if (!vis4[v[i - 1]]) { notvalid = 1; break; } } if (notvalid) puts("-1"); else { forrev(i, v.size() - 1, 0) { sort(scc[v[i]].begin(), scc[v[i]].end()); forall(j, 0, scc[v[i]].size()) if (tobevisited[scc[v[i]][j]]) printf("%d ", scc[v[i]][j]); } puts(""); } if (t) { components.clear(); v.clear(); forall(i, 0, n + 1) { vis[i] = vis2[i] = vis3[i] = vis4[i] = 0; tobevisited[i] = mark[i] = 0; graph[i].clear(); newgrph[i].clear(); revgraph[i].clear(); scc[i].clear(); } } } return 0; }