while (vertical_number, horizontal_number, current_line = gets.split.map &:to_i) != [0, 0, 0]
    horizontal_lines = Hash.new { |h,k| h[k] = {} }
    horizontal_number.times {
        height, left, right = gets.split.map &:to_i
        horizontal_lines[height][left] = right
        horizontal_lines[height][right] = left
    }
    horizontal_lines = horizontal_lines.sort.reverse.to_h

    horizontal_lines.each { |height, lines|
        current_line = horizontal_lines[height][current_line] || current_line
    }

    puts(current_line)
end

