#! ruby # yukicoder My Practice # author: Leonardone @ NEETSDKASU n = gets.to_i M = gets.to_i M.times do p, q = gets.chomp.split.map(&:to_i) if p == n n = q elsif q == n n = p end end puts n