#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
int n;
struct s{
char id[20];
int inh;
int inm;
int ins;
int outh;
int outm;
int outs;
}rec[10000000];
bool cmpin(s a,s b){
if(a.inh!=b.inh){
return a.inh<b.inh;
}else{
if(a.inm!=b.inh){
return a.inm<b.inm;
}else{
return a.ins<b.ins;
}
}
}
bool cmpout(s a,s b){
if(a.outh!=b.outh){
return a.outh>b.outh;
}else{
if(a.outm!=b.outh){
return a.outm>b.outm;
}else{
return a.outs>b.outs;
}
}
}
int main(){
freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s %d:%d:%d %d:%d:%d",
&rec[i].id,&rec[i].inh,&rec[i].inm,&rec[i].ins,&rec[i].outh,&rec[i].outm,&rec[i].outs);
}
sort(rec,rec+n,cmpin);
printf("%s ",rec[0].id);
sort(rec,rec+n,cmpout);
printf("%s\n",rec[0].id);
return 0;
}