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