import java.util.*;

/**
 * Sky net virus solution based on Breadth First Search
 * See {@link https://www.codingame.com/ide/43744749f2fe544b05a07aee8a7521d45976f5f}
 **/
class Player {

    private static Set<Integer> visited = new HashSet<Integer>();
    private static Set<Integer> gateways = new HashSet<Integer>();
    static int N = 0;
    /**
     * Adjacency matrix
     */
    private static int[][] ADJENCENCY_MATRIX;
    private static String lastCommand = null;
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        N = in.nextInt(); // the total number of nodes in the level, including the gateways
        ADJENCENCY_MATRIX = new int[N][N];


        int L = in.nextInt(); // the number of links
        int E = in.nextInt(); // the number of exit gateways


        for (int i = 0; i < L; i++) {
            int N1 = in.nextInt(); // N1 and N2 defines a link between these nodes
            int N2 = in.nextInt();
            ADJENCENCY_MATRIX[N1][N2] = 1;
            ADJENCENCY_MATRIX[N2][N1] = 1;
        }

        for (int i = 0; i < E; i++) {
            int EI = in.nextInt(); // the index of a gateway node
            gateways.add(EI);
        }

        System.err.println(gateways.iterator().next());
        // game loop
        while (true) {
            int SI = in.nextInt(); // The index of the node on which the Skynet agent is positioned this turn

            // Write an action using System.out.println()


            // Example: 0 1 are the indices of the nodes you wish to sever the link between
            Map<Integer, Queue<Integer>> queueNextLevel = new HashMap<Integer, Queue<Integer>>();
            queueNextLevel.put(SI, getChildren(SI));

            findFirstExit(queueNextLevel);
            System.out.println(lastCommand);
        }
    }

    private static Queue<Integer> getChildren(int x){
        Queue<Integer> queue = new LinkedList<Integer>();
        for (int i = 0; i < N; i++) {
            if (ADJENCENCY_MATRIX[x][i] == 1) {
                queue.add(i);
            }
        }
        return queue;
    }

    //using BFS to find the shortest path to the gateway
    public static void findFirstExit(Map<Integer, Queue<Integer>> level){
        Map<Integer, Queue<Integer>> nextLevel = new HashMap<Integer, Queue<Integer>>();
        for (Integer currentRoot: level.keySet()  ) {
            Queue<Integer> children = level.get(currentRoot);
            visited.add(currentRoot);
            while (!children.isEmpty()){
                int currentChildNode = children.remove();
                if (gateways.contains(currentChildNode)){
                    lastCommand = ""+currentRoot+" "+currentChildNode;
                }
                nextLevel.put(currentChildNode, getChildren(currentChildNode));
            }
        }
        if (!nextLevel.isEmpty()) {
            findFirstExit(nextLevel);
        }
        return;
    }
}
@@ -159,16 +159,17 @@
 976f5f%7D%0A
+%0A
  **/%0Acla
@@ -318,16 +318,24 @@
 ();%0A    
+private 
 static i
@@ -348,48 +348,8 @@
  0;%0A
-    /**%0A     * Adjacency matrix%0A     */%0A
     
@@ -370,26 +370,25 @@
  int%5B%5D%5B%5D ADJ
-EN
+A
 CENCY_MATRIX
@@ -389,32 +389,24 @@
 MATRIX;%0A    
-private 
 static Strin
@@ -426,16 +426,19 @@
  = null;
+   
 %0A    pub
@@ -615,34 +615,33 @@
 ways%0A        ADJ
-EN
+A
 CENCY_MATRIX = n
@@ -935,34 +935,33 @@
 %0A            ADJ
-EN
+A
 CENCY_MATRIX%5BN1%5D
@@ -985,18 +985,17 @@
      ADJ
-EN
+A
 CENCY_MA
@@ -1170,65 +1170,8 @@
    %7D
-%0A%0A        System.err.println(gateways.iterator().next());
 %0A   
@@ -1324,161 +1324,8 @@
 turn
-%0A%0A            // Write an action using System.out.println()%0A%0A%0A            // Example: 0 1 are the indices of the nodes you wish to sever the link between
 %0A   
@@ -1747,18 +1747,17 @@
  if (ADJ
-EN
+A
 CENCY_MA
@@ -1862,65 +1862,8 @@
  %7D%0A%0A
-    //using BFS to find the shortest path to the gateway%0A
     
@@ -2403,16 +2403,44 @@
 ldNode;%0A
+                    return;%0A
         
@@ -2659,9 +2659,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

fix innacurate refacto