#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
 int n,ti,id,tp,sc,bb,ed,tpn=-1,scn,bbn=-1,edn=-1,m1,m2,m3,m4,s1,s2,s3,s4;
 while(cin >> n) {
	 if (n==0) break;
	 tp=sc=99999; bb=ed=0;
	 for (;n>0;n--) {
		 cin >> id >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
		 ti=(m1+m2+m3+m4)*60+s1+s2+s3+s4;
		 if (tp>ti) { sc=tp; scn=tpn; tp=ti; tpn=id; }
		 if (tp<ti && sc>ti) { sc=ti; scn=id;}
		 if (ed<ti) { bb=ed; bbn=edn; ed=ti; edn=id;}
		 if (ed>ti && bb<ti) { bb=ti; bbn=id;}
	 }
	 cout << tpn << endl;
	 cout << scn << endl;
	 cout << bbn << endl;
 }
 return 0;
 }