while l = gets
  n, m, a = l.split(' ').map(&:to_i)
  break if n == 0
  tmp = []
  m.times{ tmp << gets.split(' ').map(&:to_i) }
  y = tmp.transpose[0].max
  bar = (y+1).times.map { [] }
  tmp.each{ |h, p, q| bar[h] << [p, q] }
  res = Array.new(n+1){|i| i}
  (1..y).each do |i|
    bar[i].each{ |a, b| res[a], res[b] = res[b], res[a] }
    p res
  end
  p res[1]
end