n=gets.to_i gets.to_i.times{ p,q=gets.split.map(&:to_i) if n==p n=q elsif n==q n=p end } p n