using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; //using System.Numerics; using System.Text; using System.Threading; public class Solver { List<int>[] g, rg; bool[] used; List<int> cc, order, vcc; void Dfs1(int v) { used[v] = true; foreach (int e in g[v]) if (!used[e]) Dfs1(e); order.Add(v); } bool[] toVisit; int[] map; int cn; void Dfs2(int v) { used[v] = true; map[v] = cn; cc.Add(v); if (toVisit[v]) vcc.Add(v + 1); foreach (int e in rg[v]) if (!used[e]) Dfs2(e); } HashSet<int>[] cg; List<int> top; void Dfs3(int v) { used[v] = true; foreach (int e in cg[v]) if (!used[e]) Dfs3(e); top.Add(v); } int[] rtop; bool Dfs4(int v, int target) { used[v] = true; foreach (int e in cg[v]) { if (e == target) return true; if (!used[e] && rtop[e] < rtop[target]) { if (Dfs4(e, target)) return true; } } return false; } void Solve() { for (int tt = ReadInt(); tt > 0; tt--) { int n = ReadInt(); int m = ReadInt(); int k = ReadInt(); toVisit = new bool[n]; foreach (int x in ReadIntArray()) toVisit[x - 1] = true; g = Init<List<int>>(n); rg = Init<List<int>>(n); for (; m > 0; m--) { int u = ReadInt() - 1; int v = ReadInt() - 1; g[u].Add(v); rg[v].Add(u); } used = new bool[n]; order = new List<int>(); for (int i = 0; i < n; i++) if (!used[i]) Dfs1(i); List<List<int>> comps = new List<List<int>>(); List<List<int>> vcomps = new List<List<int>>(); used = new bool[n]; map = new int[n]; cn = 0; for (int i = 0; i < n; i++) { int v = order[n - i - 1]; if (!used[v]) { cc = new List<int>(); vcc = new List<int>(); Dfs2(v); comps.Add(cc); vcc.Sort(); vcomps.Add(vcc); cn++; } } cg = Init<HashSet<int>>(cn); for (int i = 0; i < n; i++) foreach (int e in g[i]) cg[map[i]].Add(map[e]); used = new bool[cn]; top = new List<int>(); for (int i = 0; i < cn; i++) if (!used[i]) Dfs3(i); top.Reverse(); rtop = new int[cn]; for (int i = 0; i < cn; i++) rtop[top[i]] = i; int pr = -1; var ans = new List<int>(); used = new bool[cn]; bool ok = true; for (int i = 0; ok && i < cn; i++) { if (vcomps[i].Count > 0) { if (pr != -1) { if (!Dfs4(pr, i)) { ok = false; } } pr = i; ans.AddRange(vcomps[i]); } } if (ok) WriteArray(ans); else Write(-1); } } #region Main protected static TextReader reader; protected static TextWriter writer; static void Main() { #if DEBUG reader = new StreamReader("..\\..\\input.txt"); //reader = new StreamReader(Console.OpenStandardInput()); writer = Console.Out; //writer = new StreamWriter("..\\..\\output.txt"); #else reader = new StreamReader(Console.OpenStandardInput()); writer = new StreamWriter(Console.OpenStandardOutput()); //reader = new StreamReader("transform.in"); //writer = new StreamWriter("transform.out"); #endif try { //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128); //thread.Start(); //thread.Join(); new Solver().Solve(); } catch (Exception ex) { Console.WriteLine(ex); #if DEBUG #else throw; #endif } reader.Close(); writer.Close(); } #endregion #region Read / Write private static Queue<string> currentLineTokens = new Queue<string>(); private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); } public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); } public static int ReadInt() { return int.Parse(ReadToken()); } public static long ReadLong() { return long.Parse(ReadToken()); } public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); } public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); } public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); } public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); } public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; } public static int[][] ReadAndTransposeIntMatrix(int numberOfRows) { int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][]; for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret; } public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; } public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); } public static void Write(params object[] array) { WriteArray(array); } public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); } private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue> { public new TValue this[TKey key] { get { return ContainsKey(key) ? base[key] : default(TValue); } set { base[key] = value; } } } private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; } private static void Decrease(int[] a) { for (int i = 0; i < a.Length; i++) a[i]--; } #endregion }