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

struct stu{
    string name;
    int stime;
    int etime;
};
//按签到时间从小到大排序
bool cmp(stu a,stu b){
    return a.stime<b.stime;
}
//按签离时间从小到大排序
bool cmp1(stu a,stu b){
    return a.etime<b.etime;
}
int main() {
    int m;
    cin>>m;
    stu a[m];
    for(int i=0;i<m;i++){
        cin>>a[i].name;
        int h,m,s;
        scanf("%d:%d:%d ",&h,&m,&s);
        a[i].stime=h*3600+m*60+s;
        scanf("%d:%d:%d",&h,&m,&s);
        a[i].etime=h*3600+m*60+s;
    }
    sort(a,a+m,cmp);
    cout<<a[0].name<<" ";
    sort(a,a+m,cmp1);
    cout<<a[m-1].name<<endl;
    return 0;
}