#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Time{
string num;
string begin;
string end;
};
bool Compare(Time* a,Time* b){
return a->begin <b->begin;
}
bool Compare1(Time* a,Time* b){
return a->end>b->end;
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
Time* times[n];
Time* time = NULL;
string str;
getchar();
for(int i = 0;i < n; i ++){
getline(cin,str);
int index = str.find(" ");
time = new Time();
time->num = str.substr(0,index);
time->begin = str.substr(index+1,8);
time->end = str.substr(index+9);
times[i] = time;
}
sort(times,times+n,Compare);
cout << times[0]->num<<endl;
sort(times,times+n,Compare1);
cout << times[0]->num<<endl;
}
return 0;
}
09-23