N=int(input()) for _ in [0]*int(input()): P,Q=map(int,input().split()) N=((N,P)[Q==N],Q)[P==N] print(N)