#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct visitor{
char ID[20];
int in_h,in_m,in_s;
int out_h,out_m,out_s;
}v[N];
int main(){
int m,i;
struct visitor late,early;//最晚,最早
scanf("%d",&m);
for( i = 0;i < m; i++)
{
scanf("%s %d:%d:%d %d:%d:%d",v[i].ID,&v[i].in_h,&v[i].in_m,&v[i].in_s,&v[i].out_h,&v[i].out_m,&v[i].out_s);
}
late = v[0];
early = v[0];
for(i = 1;i < m; i++)
{
if(v[i].in_h < early.in_h) {
early = v[i];
}
else if(v[i].in_h == early.in_h) {
if(v[i].in_m < early.in_m){
early = v[i];
}
else if(v[i].in_m == early.in_m){
if(v[i].in_s < early.in_s ){
early = v[i];
}
}
}
if(v[i].out_h > late.out_h) {late = v[i];}
else if(v[i].out_h == late.out_h){
if(v[i].out_m > late.out_m){
late = v[i];
}
else if(v[i].out_m == late.out_m){
if(v[i].out_s > late.out_s)
{
late = v[i];
}
}
}
}
printf("%s %s",early.ID,late.ID);
return 0;
}