a=input() b=int(input()) for i in range(b): c=input().split() if c[0]==a: a=c[1] elif c[1]==a: a=c[0] print(a)