N = input() M = int(input()) for _ in range(M): l, r = input().split() if N == l: N = r elif N == r: N = l print(N)