#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;


typedef struct Re{
    string num;
    int h1 ,m1,s1;
    int h2,m2,s2;

}RE;

RE r[1010];

bool cmp(RE r1,RE r2)
{
    if(r1.h1!=r2.h1)return r1.h1<r2.h1;
    else if(r1.m1!=r1.m1)return r1.m1<r2.m1;
    else return r1.s1<r2.s1;
}
bool cmp1(RE r1,RE r2)
{
    if(r1.h2!=r2.h2)return r1.h2>r2.h2;
    else if(r1.m2!=r2.m2)return r1.m2>r2.m2;
    return r1.s2>r2.s2;
}

int main() {
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        string s1;
        cin>>s1;
        r[i].num = s1;
        int h,m,s;
        scanf("%d:%d:%d",&h,&m,&s);
        r[i].h1 = h;
        r[i].m1 =m;
        r[i].s1 = s;
        scanf("%d:%d:%d",&h,&m,&s);
        r[i].h2 = h;
        r[i].m2 =m;
        r[i].s2 = s;
    }
     sort(r,r+n,cmp);
     cout<<r[0].num<<" ";
    sort(r,r+n,cmp1);
 //   for(int i=0;i<n;i++)cout<<r[i].num<<" "<<r[i].m2<<endl;
    cout<<r[0].num<<" ";








}
// 64 位输出请用 printf("%lld")