#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int n,teamnum,b;
	int m1,s1,m2,s2,m3,s3,m4,s4;
	while(1){
		cin >> n;
		if(n==0) break;
		int team[1000000]={0};
		int team2[1000000]={0};
		for(int i=0;i<n;i++){
			cin >> teamnum;
			cin >> m1 >> s1;
			cin >> m2 >> s2;
			cin >> m3 >> s3;
			cin >> m4 >> s4;
			team[teamnum] = m1*60+s1+m2*60+s2+m3*60+s3+m4*60+s4;
			team2[teamnum]=team[teamnum];
		}
		sort(team,team+1000000);
		int cnt=0;
		for(int i=0;i<1000000;i++){
			if(team[i]!=0 && cnt<2){
				for(int j=0;j<1000000;j++){
					if(team2[j]==team[i]){
						cout << j << endl;
						cnt++;
					}
					if(team[999998]==team2[j]){
						b=j;
					}
				}
			}
		}
		cout << b << endl;
	}
	return 0;
}
		