#yuki_429 n,k,x=map(int,raw_input().split()) s=[int(i) for i in xrange(n+1)] r=[raw_input().split() for i in xrange(k)] c=map(int,raw_input().split()) c.insert(0,0) for i in xrange(len(r)): if r[i][0]=='?': break a,b=int(r[i][0]),int(r[i][1]) s[a],s[b]=s[b],s[a] for i in xrange(len(r)-1,-1,-1): if r[i][0]=='?': break a,b=int(r[i][0]),int(r[i][1]) c[a],c[b]=c[b],c[a] res=[] for i in xrange(n+1): if s[i]!=c[i]: res.append(i) print res[0],res[1]