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