#include<stdio.h>
#include<string.h>

/*
思路:
    1.创建结构性变量pNode,内部成员为id和时间;
    2.创建比较函数great(a,b),a的时间>b的时间话返回true; 
    3.输入时,把签到时间和签退时间分开录入,录入签到时间时,利用比较函数great()来获得答案; 

*/ 


using namespace std;

struct pNode{
    char id[16];
    int hh,mm,ss;
}temp,ans1,ans2;    //ans1保存答案1,ans2保存答案2 

bool great(pNode a,pNode b){    //a的时间大于b的时间返回true 
    if(a.hh != b.hh) return a.hh>b.hh;
    if(a.mm != b.hh) return a.mm>b.mm;
    return a.ss >=b.ss;

} 

int main(){

    int n;
    scanf("%d",&n);

    ans1.hh = 24;    ans1.mm = 60;    ans1.ss = 60;    //初始化签到时间为最大值 
    ans2.hh = 0;    ans2.mm = 0;    ans2.ss = 0;    //初始化签退时间为最小值 

    for(int i=0;i<n;i++){
        scanf("%s %d:%d:%d",temp.id,&temp.hh,&temp.mm,&temp.ss);

        if(great(ans1,temp))    ans1 = temp;

        scanf("%d:%d:%d",&temp.hh,&temp.mm,&temp.ss);

        if(great(temp,ans2))    ans2 = temp;
    }

    printf("%s %s",ans1.id,ans2.id);


    return 0;
}