Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Id int
	Time int
}

type ByTime []Team
func (a ByTime) Len() int {
	return len(a)
}
func (a ByTime) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}
func (a ByTime) Less(i, j int) bool {
	return a[i].Time < a[j].Time
}

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		teams := make([]Team, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			id, _ := strconv.Atoi(buf[0])
			m1, _ := strconv.Atoi(buf[1])
			s1, _ := strconv.Atoi(buf[2])
			m2, _ := strconv.Atoi(buf[3])
			s2, _ := strconv.Atoi(buf[4])
			m3, _ := strconv.Atoi(buf[5])
			s3, _ := strconv.Atoi(buf[6])
			m4, _ := strconv.Atoi(buf[7])
			s4, _ := strconv.Atoi(buf[8])
			team := Team{id, m1*60+s1+m2*60+s2+m3*60+s3+m4*60+s4}
			teams[i] = team
		}
		sort.Sort(ByTime(teams))
		fmt.Println(teams[0].Id)
		fmt.Println(teams[1].Id)
		fmt.Println(teams[len(teams)-2].Id)
	}
}

Python: while 1:
    n=int(input())
    if n==0:break
    a={}
    for _ in range(n):
        b=list(map(int,input().split()))
        a[b[0]]=sum(b[1:8:2])*60+sum(b[2:9:2])
    a=sorted(a.items(),key=lambda x:x[1])
    for i in [0,1,-2]:print(a[i][0])

Python: while 1:
    n=int(input())
    if n==0:break
    a={}
    for _ in range(n):
        b=list(map(int,input().split()))
        a[b[0]]=sum(b[1:8:2])*60+sum(b[2:9:2])
    a=sorted(a.items(),key=lambda x:x[1])
    for i in [0,1,-2]:print(a[i][0])

Python: # -*- coding: utf-8 -*-

import sys
import os

for s in sys.stdin:
    N = int(s)
    if N == 0:
        break

    A = []
    for i in range(N):
        lst = list(map(int, input().split()))
        id = lst[0]
        data = lst[1:]
        time_sum = data[0] * 60 + data[1] + \
                   data[2] * 60 + data[3] + \
                   data[4] * 60 + data[5] + \
                   data[6] * 60 + data[7]
        A.append((time_sum, id))
    A.sort()

    print(A[0][1])
    print(A[1][1])
    print(A[-2][1])

Python: while 1:
    n = int(input())
    team = []
    for _ in range(n):
        i, *T = map(int, input().split())
        t = 0
        for m, s in zip(T[::2], T[1::2]):
            t += m*60 + s
        team.append((t, i))
    team.sort()
    print(team[0][1])
    print(team[1][1])
    print(team[-2][1])

Python: while True:
  n =int(input())
  if n==0:
    break
  A=[]
  for i in range(n):
    id,m1,s1,m2,s2,m3,s3,m4,s4=map(int, input().split())
    time=(m1+m2+m3+m4)*60+s1+s2+s3+s4
    A.append((time,id))
  A.sort()
  print(A[0][1])
  print(A[1][1])
  print(A[n-2][1])

Python: while True:
    num = int(input())
    if num == 0:
        break
    L = []
    for i in range(num):
        id, m1, s1, m2, s2, m3, s3, m4, s4 = [int(x) for x in input().split()]
        t = (m1 + m2 + m3 + m4) *60 + s1 + s2 + s3 + s4
        L.append([t, id])
    L.sort()
    print(L[0][1])
    print(L[1][1])
    print(L[-2][1])

Python: while 1:
    n = int(input())
    if n == 0:
        break

    score = []
    for _ in range(n):
        datas = list(map(int, input().split()))
        num = datas.pop(0)

        time = 0
        for _ in range(4):
            time += datas.pop(0) * 60 + datas.pop(0)

        score.append([num, time])

    score = sorted(score, key=lambda x: x[1])
    print(score[0][0])
    print(score[1][0])
    print(score[-2][0])

Python: # from sys import exit
while(True):
    N = int(input())
    if N == 0:
        break
    L = []
    for i in range(N):
        r = [int(n) for n in input().split()]
        time = sum([r[i]*60 + r[i+1] for i in range(1, 9, 2)])
        L.append((r[0], time))
        # print(abs(22- w/((h/100)**2)))
    L = sorted(L, key=lambda x: x[1])
    print(L[0][0])
    print(L[1][0])
    print(L[-2][0])

Python: # AOJ 0161 Sport Meet
# Python3 2018.6.18 bal4u

while True:
	n = int(input())
	if n == 0: break
	team = {}
	for i in range(n):
		a = list(map(int, input().split()))
		s = 0
		for j in range(1, 8, 2):
			s += 60*a[j] + a[j+1]
		team[a[0]] = s
	ans = sorted(team.items(), key=lambda x: x[1])
	print(ans[0][0], ans[1][0], ans[n-2][0], sep='\n')

Python: while True:
    n = int(input())
    if n==0:break
    d={}
    for i in range(n):
        c,q,w,e,r,t,y,u,o = input().split()
        d[c] = (int(q)+int(e)+int(t)+int(u))*60+int(w)+int(r)+int(y)+int(o)
    ans = sorted(d.items(), key=lambda x: x[1])
    for j in [0,1,-2]:
        a,b = ans[j]
        print(a)

Python: def main():
    while True:
        n = int(input())
        if n == 0: break

        res = []
        for i in range(n):
            row = input().split()
            res.append([row[0], 0])
            s_m = 0
            s_s = 0
            for j in range(1, 9):
                if j % 2 == 0:
                    s_s += int(row[j])
                else:
                    s_m += int(row[j])
            res[i][1] = s_s + s_m * 60

        ans = sorted(res, key=lambda x: x[1])
        print(ans[0][0])
        print(ans[1][0])
        print(ans[-2][0])

if __name__ == "__main__":
    main()

Python: while True:
  n = int(input())
  if n == 0:
    break
  pairs = []
  for _ in range(n):
    i, m1, s1, m2, s2, m3, s3, m4, s4 = map(int, input().split())
    score = (m1 + m2 + m3 + m4) * 60 + (s1 + s2 + s3 + s4)
    pairs.append((score, i))
  pairs.sort()
  print(pairs[0][1])
  print(pairs[1][1])
  print(pairs[-2][1])

Python: while True:
    n = int(input())
    if n == 0: break
    record = {}
    for _ in range(n):
        num, m1, s1, m2, s2, m3, s3, m4, s4 = \
            map(int, input().split())
        record[num] = (m1*60+s1) + (m2*60+s2) + \
                        (m3*60+s3) + (m4*60+s4)
    record = sorted(record.items(), key=lambda x: x[1])
    result = record[:2]
    result.append(record[-2])
    for res in result:
        print(res[0])

Python: while True:
    n = int(input())
    if n==0:break
    d = dict()
    for _ in range(n):
        l = list(map(int, input().split()))
        a, b = divmod((l[2]+l[4]+l[6]+l[8]), 60)
        d[l[0]] = (l[1]+l[3]+l[5]+l[7])*60 + a*60 + b
    for i in [0,1,-2]: print(sorted(d.items(), key=lambda x: x[1])[i][0])

Python: while 1:
    n = int(input())
    if n == 0:
        break
    x = {}
    for i in range(n):
        ID,m1,s1,m2,s2,m3,s3,m4,s4 = map(int,input().split())
        r = m1*60+s1 + m2*60+s2 + m3*60+s3 + m4*60+s4
        x[ID] = r
    x = sorted(x.items(), key=lambda i: i[1])
    print(x[0][0])
    print(x[1][0])
    print(x[n-2][0])

Python: while True:
    n = int(input())
    if n == 0:
        break
    Run = []
    for i in range(n):
        num,m_1,s_1,m_2,s_2,m_3,s_3,m_4,s_4 = map(int,input().split())
        Run.append([(m_1 + m_2 + m_3 + m_4)*60 +s_1 +s_2+s_3+s_4,num])
    Run = sorted(Run)
    print(Run[0][1])
    print(Run[1][1])
    print(Run[len(Run) - 2][1])

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0161

"""
import sys
from sys import stdin
input = stdin.readline
from collections import defaultdict


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        data = []
        for _ in range(n):
            id, m1, s1, m2, s2, m3, s3, m4, s4 = [int(x) for x in input().split()]
            total_time = (m1 + m2 + m3 + m4) * 60 + (s1 + s2 + s3 + s4)
            data.append([total_time, id])
        data.sort()

        print(data[0][1])
        print(data[1][1])
        print(data[-2][1])


if __name__ == '__main__':
    main(sys.argv[1:])

Python: while 1:
    n = int(input())
    team = []
    for _ in range(n):
        i, *T = map(int, input().split())
        t = 0
        for m, s in zip(T[::2], T[1::2]):
            t += m*60 + s
        team.append((t, i))
    team.sort()
    print(*[team[0][1], team[1][1], team[-2][1]], sep='\n')