#include<bits/stdc++.h>
using namespace std;
int mina=25,minb=25,minc=25;
int maxa=-1,maxb=-1,maxc=-1;
string minstr,maxstr;
int m;
int main(){
cin>>m;
while(m--){
string s,s1,s2;
int a,b,c,d,e,f;
cin>>s>>s1>>s2;
a=stoi(s1.substr(0,2));
b=stoi(s1.substr(3,2));
c=stoi(s1.substr(6,2));
d=stoi(s2.substr(0,2));
e=stoi(s2.substr(3,2));
f=stoi(s2.substr(6,2));
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<endl;
if(mina>a){
minstr=s;
mina=a,minb=b,minc=c;
}else if(mina==a){
if(minb>b){
minstr=s;
mina=a,minb=b,minc=c;
}else if(minb==b){
if(minc>c){
minstr=s;
mina=a,minb=b,minc=c;
}
}
}
if(maxa<d){
maxstr=s;
maxa=d,maxb=e,maxc=f;
}else if(maxa==d){
if(maxb<e){
maxstr=s;
maxa=d,maxb=e,maxc=f;
}else if(maxb==e){
if(maxc<f) {
maxstr=s;maxa=d,maxb=e,maxc=f;
}
}
}
}
cout<<minstr<<" "<<maxstr;
return 0;
}