#include <iostream>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;

int n, k, x;
int a[100000], b[100000];
int c[100000];
int d[100000];

int main()
{
	int i;
	cin >> n >> k >> x;
	x--;					//0-indexedにする
	for( i = 0; i < k; i++ ){
		if( i == x ){
			char s[10], s2[10];
			cin >> s >> s2;
		}
		else{
			cin >> a[i] >> b[i];
			a[i]--; b[i]--;	//0-indexedにする
		}
	}
	for( i = 0; i < n; i++ ){
		cin >> c[i];
	}
	
	for( i = 0; i < n; i++ ){
		d[i] = i + 1;
	}
	
	for( i = 0; i < x; i++ ){
		swap(d[a[i]], d[b[i]]);
	}
	for( i = k - 1; i > x; i-- ){
		swap(c[a[i]], c[b[i]]);
	}
	
	vector<int> out;
	for( i = 0; i < n; i++ ){
		if( c[i] != d[i] ){
			out.push_back(i);
		}
	}
	cout << out[0] + 1 << " " << out[1] + 1 << endl;
	return 0;
}