import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int K = scan.nextInt(); int X = scan.nextInt() - 1; int[]A = new int[K]; int[]B = new int[K]; for(int i = 0; i < K; i++) { if(i == X) { scan.next(); scan.next(); A[i] = 0; B[i] = 0; }else { A[i] = scan.nextInt() - 1; B[i] = scan.nextInt() - 1; } } int[]C = new int[N]; int[]C1 = new int[N]; for(int i = 0; i < N; i++) { C[i] = scan.nextInt(); C1[i] = i + 1; } scan.close(); for(int i = 0; i < X; i++) { swap(C1, A[i], B[i]); } for(int i = K - 1; i > X; i--) { swap(C, A[i], B[i]); } ArrayList list = new ArrayList(); for(int i = 0; i < N; i++) { if(C[i] != C1[i]) { list.add(i + 1); } } System.out.println(list.get(0) + " " + list.get(1)); } static void swap(int[]A, int i, int j) { int t = A[j]; A[j] = A[i]; A[i] = t; } }