/* * * File: stuff.cpp * Author: Andy Y.F. Huang (azneye) * Created on Aug 23, 2014, 11:50:25 PM */ #include <bits/stdc++.h> using namespace std; namespace stuff { typedef long long ll; const int MAX = 111000; vector<int> adj[MAX], rev[MAX]; int comp[MAX]; bool vis[MAX]; stack<int> sta; void dfs1(int at) { if (vis[at]) { return; } vis[at] = true; for (const int& to : adj[at]) { dfs1(to); } sta.push(at); } void dfs2(int at, int c_val) { if (vis[at]) { return; } vis[at] = true; comp[at] = c_val; for (const int& to : rev[at]) { dfs2(to, c_val); } } void solve(int test_num) { int V, E, K; cin >> V >> E >> K; for (int i = 1; i <= V; ++i) { adj[i].clear(); rev[i].clear(); } vector<int> need(K); for (int i = 0; i < K; ++i) { cin >> need[i]; } for (int e = 0, a, b; e < E; ++e) { cin >> a >> b; adj[a].push_back(b); rev[b].push_back(a); } memset(vis, false, sizeof(vis)); for (int v = 1; v <= V; ++v) { if (!vis[v]) { dfs1(v); } } memset(vis, false, sizeof(vis)); int C = 0; while (!sta.empty()) { if (!vis[sta.top()]) { dfs2(sta.top(), C++); } sta.pop(); } vector<vector<int> > in_comp(C), scc(C); for (const int& v : need) { in_comp[comp[v]].push_back(v); } for (int c = 0; c < C; ++c) { sort(in_comp[c].begin(), in_comp[c].end()); } for (int v = 1; v <= V; ++v) { for (const int& to : adj[v]) { if (v != to) { scc[comp[v]].push_back(comp[to]); } } } int st = 0; while (in_comp[st].empty()) { ++st; } vector<int> res; for (const int& v : in_comp[st]) { res.push_back(v); } memset(vis, false, sizeof(vis)); //pln(in_comp); //pln(scc); vis[st] = true; for (int c = st + 1, pred = st; c < C; ++c) { if (in_comp[c].empty()) { continue; } for (int at = pred; at < c; ++at) { if (!vis[at]) { continue; } for (const int& to : scc[at]) { if (to <= c) { vis[to] = true; } } } if (!vis[c]) { cout << "-1\n"; return; } pred = c; for (const int& v : in_comp[c]) { res.push_back(v); } } for (int i = 0; i < K; ++i) { cout << res[i] << ' '; } cout << endl; } void solve() { #ifdef AZN //make_case(); double start_t = clock(); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); //freopen("azn.txt", "w", stderr); #endif ios::sync_with_stdio(false); cin.tie(NULL); int T = 1; //scanf("%d", &T); cin >> T; for (int t = 1; t <= T; t++) solve(t); #ifdef AZN cerr << fixed << setprecision(3) << "Took: " << ((clock() - start_t) / CLOCKS_PER_SEC); #endif } } int main() { stuff::solve(); return 0; }