cup = input() num = int(input()) for i in range(num): a, b = input().split() if cup == a: cup = b elif cup == b: cup = a print(cup)