N = input() M = int(input()) for v in range(1, M+1): f = input() s_f = f.split() s = ''.join(s_f) if N in s: N = s.strip(N) print(int(N))