// #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef map<int, int> MAPII;

#define MP make_pair
#define fastIO	cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (未test)
// #define FOREACH_IT(it,c)	for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(), c_end=c.end(); it!=c_end; ++it)

void solveAndOutput(MAPII result){
	int count = 0;
	MAPII::iterator it_end = result.end();
	it_end--; it_end--;
	foreach_it(MAPII, it, result){
		count++;
		if (count <= 2)
			cout << (*it).second << endl;
		if (it == it_end)
			cout << (*it).second << endl;
	}
}

int main(){
	int n;
	int c1, m1, s1, m2, s2, m3, s3, m4, s4;
	MAPII result;
	fastIO;

	while (cin >> n, n){
		while (n--){
			cin >> c1 >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
			result.insert(MP(m1 * 60 + s1 + m2 * 60 + s2 + m3 * 60 + s3 + m4 * 60 + s4, c1));
		}
		solveAndOutput(result);
		result.clear();
	}
	return 0;
}