#include<iostream>
using namespace std;

int main()
{
    int a[100000], sum[100000], b, c, n;

    while( cin >> n )
    {
        if( n == 0 )
            break;

        for( int i = 0; i < 100000; i++ )
            sum[i] = 0;

        for( int i = 0; i < n; i++ )
        {
            cin >> a[i];
            for( int j = 0; j < 4; j++ )
            {
                cin >> b >> c;
                sum[i] += b * 60 + c;
            }
        }

        for( int j = 0; j < 2; j++ )
        {
            int min = 1000000, max = 0, s = 0, t = 0;
            for( int i = 0; i < n; i++ )
            {
                if( a[i] != -1 && min > sum[i] )
                {
                    min = sum[i];
                    s = i;
                }
                if( a[i] != -1 && max < sum[i] )
                {
                    max = sum[i];
                    t = i;
                }
            }
            cout << a[s] << endl;
            if( j == 1 )
                cout << a[t] << endl;
            a[s] = -1;
            a[t] = -1;
        }
    }
    return 0;
}

