#include <iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main() {
int n;
while (scanf("%d",&n)!=EOF&&n>0) {
getchar();
string early="99:99:99";//用于对比早班时间
string earlypeople="";//用于保存开门人
string lately="00:00:00";//用于对比晚班时间
string latepeople="";//用于保存关门人
for(int i=0;i<n;i++){
string s;
getline(cin,s);
//切记每个字符串后面有个换行符需要读取,我在这里报错纠结了很久
int first=s.find(' ');
//查找号码和日期之间的空格
string name=s.substr(0,first);
string front_time=s.substr(first+1,8);
//前面的日期
string rear_time=s.substr(first+10);
//后面的日期
if(front_time<early){
early=front_time;
earlypeople=name;
}
if(rear_time>lately){
latepeople=name;
lately=rear_time;
}
}
cout<<earlypeople<<" ";
cout<<latepeople<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")