loop do
    n = gets.to_i
    break if n == 0
    a = (1..n).map { gets.split.map(&:to_i) }
    a.sort_by! {|_, *b|
        b.each_slice(2).map {|m, s| 60 * m + s}.inject(:+)
    }
    p a[0][0]
    p a[1][0]
    p a[-2][0]
end
