import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class Main { public static long MOD = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int K = sc.nextInt(); final int X = sc.nextInt() - 1; int[][] numbers = new int[K][2]; for(int i = 0; i < K; i++){ if(i == X){ sc.next(); sc.next(); numbers[i][0] = numbers[i][1] = -1; }else{ numbers[i][0] = sc.nextInt() - 1; numbers[i][1] = sc.nextInt() - 1; } } int[] forward = new int[N]; for(int i = 0; i < N; i++){ forward[i] = i; } int[] backward = new int[N]; for(int i = 0; i < N; i++){ backward[i] = sc.nextInt() - 1; } for(int i = 0; i < X; i++){ final int a = numbers[i][0]; final int b = numbers[i][1]; { int tmp = forward[a]; forward[a] = forward[b]; forward[b] = tmp; } } for(int i = K - 1; i > X; i--){ final int a = numbers[i][0]; final int b = numbers[i][1]; { int tmp = backward[a]; backward[a] = backward[b]; backward[b] = tmp; } } //System.out.println(Arrays.toString(forward)); //System.out.println(Arrays.toString(backward)); int diff_A = -1, diff_B = -1; for(int i = 0; i < N; i++){ if(forward[i] == backward[i]){ continue; } if(diff_A < 0){ diff_A = i + 1; }else{ diff_B = i + 1; } } System.out.println(diff_A + " " + diff_B); } }