v=gets.to_i gets.to_i.times do a,b=gets.split.map(&:to_i) v=b if a==v v=a if b==v end p v