#include <stdio.h>
struct person {
char id[16];
int h, m, s;
}tmp, unlock, lock;
int earlier(struct person p1, struct person p2){
if(p1.h != p2.h)
return p1.h < p2.h;
else if(p1.m != p2.m)
return p1.m < p2.m;
else
return p1.s < p2.s;
}
int main(){
int N;
scanf("%d", &N);
unlock.h = 24;
unlock.m = unlock.s = 60;
lock.h = lock.m = lock.s = 0;
for(int i = 0; i < N; i++){
scanf("%s %d:%d:%d", tmp.id, &tmp.h, &tmp.m, &tmp.s);
if(earlier(tmp, unlock))
unlock = tmp;
scanf("%d:%d:%d", &tmp.h, &tmp.m, &tmp.s);
if(!earlier(tmp, lock))
lock = tmp;
}
printf("%s %s", unlock.id, lock.id);
return 0;
}