using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        for (string line; (line = Console.ReadLine()) != "0";) {
            var n = int.Parse(line);
            var teams = new Dictionary<int, int>();

            for (var i = 0; i < n; i++) {
                var l = Console.ReadLine().Split();
                var id = int.Parse(l[0]);
                var time = int.Parse(l[1]) * 60 + int.Parse(l[2])
                         + int.Parse(l[3]) * 60 + int.Parse(l[4])
                         + int.Parse(l[5]) * 60 + int.Parse(l[6])
                         + int.Parse(l[7]) * 60 + int.Parse(l[8]);

                teams.Add(id, time);
            }

            var sortedTeams = teams.OrderBy(kv => kv.Value).ToArray();
            Console.WriteLine(sortedTeams[0]);
            Console.WriteLine(sortedTeams[1]);
            Console.WriteLine(sortedTeams[n - 2]);
        }
    }
}