n, m = io.read("*n", "*n") for i = 1, m do p, q = io.read("*n", "*n") if(n == p) then n = q elseif(n == q) then n = p end end print(n)