#include<cstdio>
struct acw{
char id[16] ;
int hh,mm,ss;
}first,last,temp;
bool cmp (acw a,acw b)
{
if(a.hh!=b.hh ) return a.hh >b.hh ;
if(a.mm!=b.mm ) return a.mm >b.mm ;
else return a.ss>b.ss;
}
int main()
{
int N,i;
first.hh= 24,first.mm=60,first.ss=60;
last.hh=0,last.mm=0,last.ss=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%s %d:%d:%d",temp.id,&temp.hh,&temp.mm,&temp.ss );
if(cmp(temp,first)==false) first=temp;
scanf("%d:%d:%d",&temp.hh,&temp.mm,&temp.ss);
if(cmp(temp,last)==true) last=temp;
}
printf("%s %s",first.id,last.id);
return 0;
//2018年7月22日23:57:47 我对你的爱每一分都不会少
}