#include using namespace std; int main(){ char drop; int n, k, x; cin >> n >> k >> x; int ans[2] = {-1}, a[k], b[k], c[n], cop[n]; for(int i = 0; i < k; i++){ if(i == x - 1){ cin >> drop >> drop; a[i] = b[i] = -1; continue; } cin >> a[i] >> b[i]; a[i]--; b[i]--; } for(int i = 0; i < n; i++){ cin >> c[i]; cop[i] = i + 1; } for(int i = 0; a[i] != -1; i++) swap(cop[a[i]], cop[b[i]]); for(int i = k; a[--i] != -1;) swap(c[a[i]], c[b[i]]); for(int i = 0; i < n; i++) if(cop[i] != c[i]) ans[ans[0] != -1] = i + 1; cout << ans[0] << ' ' << ans[1] << endl; }