n = int(input())
d = dict()
for i in n:
    syn1, syn2 = input().split()
    d[syn1] = syn2
    d[syn2] = syn1
print(d[input()])

14381c1ade55cba85456a9274ac4153a2f9d6b51
<mask token>

<mask token>
for i in n:
    syn1, syn2 = input().split()
    d[syn1] = syn2
    d[syn2] = syn1
print(d[input()])

n = int(input())
d = dict()
for i in n:
    syn1, syn2 = input().split()
    d[syn1] = syn2
    d[syn2] = syn1
print(d[input()])
