def gets_nums; gets.split(' ').map(&:to_i) end
until (n, m, a = gets_nums) == [0, 0, 0]
  puts m.times
    .lazy
    .map { gets_nums }
    .sort_by {|(h, *_)| -h }
    .reduce(a) {|current, (h, *pq)|
      i = pq.index(current)
      i.nil? ? current : pq[i - 1]
    }
end