#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

struct hoge{
    ll s, n;
    hoge(ll s, ll n) : s(s), n(n) { }
};
bool operator < (const hoge &a, const hoge &b){
    return a.n != b.n ? a.n < b.n : a.s < b.s;
}

int main(void){
    while(true){
        ll n;
        cin >> n;
        if(n==0) break;
        vector<hoge> v;
        ll id, m1, s1, m2, s2, m3, s3, m4, s4;
        
        for(ll i=0; i<n; i++){
            cin >> id >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
            ll sum=60*m1+s1+60*m2+s2+60*m3+s3+60*m4+s4;
            v.push_back(hoge(id, sum));
        }
        sort(v.begin(), v.end());
        
        cout << v[0].s << "\n";
        cout << v[1].s << "\n";
        cout << v[n-2].s << "\n";
         
    }
     
     
    return 0;
}
