// Modification to the below source code // Source: http://www.sanfoundry.com/java-program-check-connectivity-graph-using-dfs/ import java.io.*; import java.util.Scanner; import java.util.Stack; import java.util.Arrays; public class Solution { private final int vertices; private int[][] adjacency_matrix; private Stack<Integer> stack; private static int[] val; private static int len; public Solution(int v) { vertices = v; adjacency_matrix = new int[vertices + 1][vertices + 1]; stack = new Stack<Integer>(); } public void makeEdge(int to, int from, int edge) { try { adjacency_matrix[to][from] = edge; //adjacency_matrix[from][to] = edge; } catch (ArrayIndexOutOfBoundsException index) { System.out.println("The vertices does not exists"); } } public int getEdge(int to, int from) { try { return adjacency_matrix[to][from]; } catch (ArrayIndexOutOfBoundsException index) { System.out.println("The vertices does not exists"); } return -1; } public boolean dfs(int source) { int number_of_nodes = adjacency_matrix[source].length - 1; int[] visited = new int[number_of_nodes + 1]; int i, element; visited[source] = 1; stack.push(source); while (!stack.isEmpty()) { element = stack.pop(); i = 1;// element; while (i <= number_of_nodes) { if (adjacency_matrix[element][i] == 1 && visited[i] == 0) { stack.push(i); visited[i] = 1; } i++; } } int count = 0; String temp = " "; for (int v = 1; v <= number_of_nodes; v++) if (visited[v] == 1) { temp += v + " "; count++; } for(int z=0;z<len;z++) if(temp.indexOf(" "+val[z]+" ") == -1) return false; System.out.print(source); for(int z=0;z<len;z++) { if(val[z]==source) continue; System.out.print(" "+val[z]); } System.out.println(); return true; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(br.readLine()); while(tc-- > 0) { String line1[] = br.readLine().split(" "); Solution graph; int v = Integer.parseInt(line1[0]); int e = Integer.parseInt(line1[1]); len = Integer.parseInt(line1[2]); String line2[] = br.readLine().split(" "); val = new int[len]; for(int i=0;i<len;i++) val[i] = Integer.parseInt(line2[i]); Arrays.sort(val); int count=1; graph = new Solution(v); while (count <= e) { String line3[] = br.readLine().split(" "); int to = Integer.parseInt(line3[0]); int from = Integer.parseInt(line3[1]); graph.makeEdge(to, from, 1); count++; } boolean flag = false; for(int i=1;i<=v;i++) { //int sourceNode = val[i]; int sourceNode = i; if(graph.dfs(sourceNode)) { flag = true; break; } } if(!flag) System.out.println("-1"); } } }