import java.util.*; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws Exception { int n = sc.nextInt(); int k = sc.nextInt(); int x = sc.nextInt(); int[] ary = new int[n+1]; int[] tmp = new int[n+1]; for (int i = 0;i <= n;i++) { ary[i] = i; } for (int i = 0;i < k;i++) { String a = sc.next(); String b = sc.next(); if (a.equals("?")) { tmp = (int[]) ary.clone(); continue; } swap(ary, a, b); } List list = new ArrayList<>(); for (int i = 1;i <= n;i++) { if (ary[i] != sc.nextInt()) list.add(ary[i]); if (list.size() == 2) break; } int[] ret = new int[3]; for (int i = 1;i <= n;i++) { if (list.get(0) == tmp[i]) ret[1] = i; if (list.get(1) == tmp[i]) ret[2] = i; } Arrays.sort(ret); System.out.println(ret[1] + " " + ret[2]); } private static void swap(int[] ary, String a, String b) { int ai = Integer.parseInt(a); int bi = Integer.parseInt(b); int temp; temp = ary[ai]; ary[ai] = ary[bi]; ary[bi] = temp; } }