n = input() m = int(input()) for i in range(m): p,q = input().split() if n==p: n = q elif n==q: n = p print(n)