n = gets.to_i
until n == 0
    score_board = Hash.new
    n.times do
        x = gets.split(" ").map{|k| k.to_i }
        time_sum = 0
        for i in 1..8 do
            if i % 2 == 1
                time_sum += x[i] * 60
            elsif i % 2 == 0
                time_sum += x[i]
            end
        end
        score_board[x[0]] = time_sum
    end
    score_board1 = score_board.sort_by{ |_, v| v }
        Hash[*score_board1.first].each do |k,v|
            puts k
        end
        score_board1.shift
        Hash[*score_board1.first].each do |k,v|
            puts k
        end
        Hash[*score_board1.to_a.at(score_board1.size-2)].each do |k,v|
            puts k
        end
    n = gets.to_i 
end
