
#include <bits/stdc++.h>
#include<math.h>
using namespace std;
const int MAX_LEN = 100005;
struct Person{
char name[15];
int HH;
int MM;
int SS;
int HH1;
int MM1;
int SS1;
}early,lately;
int main(){
early.HH = 24;
early.MM = 0;
early.SS = 0;
lately.HH = 0;
lately.MM = 0;
lately.SS = 0;
int n;
cin>>n;
for(int i=0;i<n;++i){
Person temp;
scanf("%s %d:%d:%d %d:%d:%d",temp.name,&temp.HH,&temp.MM,&temp.SS,&temp.HH1,&temp.MM1,&temp.SS1);
if(temp.HH<early.HH || (temp.HH == early.HH && temp.MM<early.MM)||(temp.HH==early.HH && temp.MM == early.MM && temp.SS<early.SS)){
early.HH = temp.HH;
early.MM = temp.MM;
early.SS = temp.SS;
//early.name = temp.name;
memset(early.name,'\0',sizeof(early.name));
strcpy(early.name, temp.name);
}
if(temp.HH1>lately.HH || (temp.HH1 == lately.HH && temp.MM1>lately.MM)||(temp.HH1==lately.HH && temp.MM1 == lately.MM && temp.SS1>lately.SS)){
lately.HH = temp.HH1;
lately.MM = temp.MM1;
lately.SS = temp.SS1;
memset(lately.name,'\0',sizeof(lately.name));
strcpy(lately.name,temp.name);
}
}
cout<<early.name<<" "<<lately.name<<endl;
system("pause");
return 0;
}