n=STDIN.gets.chomp m=STDIN.gets.to_i m.times do |i| pa,qa=STDIN.gets.chomp.split(" ") if pa==n n=qa elsif qa==n n=pa end end puts n