#include<bits/stdc++.h>
using namespace std;
struct people{
string id;
string temp;
int hour;
int m;
int s;
int enhour;
int enm;
int ens;
}p[20];
bool cmp(people p1,people p2)
{
if(p1.hour!=p2.hour) return p1.hour<p2.hour;
else
{
if(p1.m!=p2.m) return p1.m<p2.m;
else return p1.s<p2.s;
}
}
bool cmp1(people p1,people p2)
{
if(p1.enhour!=p2.enhour) return p1.enhour<p2.enhour;
else
{
if(p1.enm!=p2.enm) return p1.enm<p2.enm;
else return p1.ens<p2.ens;
}
}
int main()
{
int n;
cin>>n;
getchar();
for(int i=0;i<n;i++)
{
string a;
getline(cin,a);
int temp=0;
int l=a.size()-18;
for(int j=0;j<l;j++)
{
p[i].id+=a[j];
}
for(int j=l+1;j<=l+2;j++)
{
p[i].temp+=a[j];
}
p[i].hour=stoi(p[i].temp);
p[i].temp="";
for(int j=l+4;j<=l+5;j++) p[i].temp+=a[j];
p[i].m=stoi(p[i].temp);
p[i].temp="";
for(int j=l+7;j<=l+8;j++) p[i].temp+=a[j];
p[i].s=stoi(p[i].temp);
p[i].temp="";
for(int j=l+10;j<=l+11;j++) p[i].temp+=a[j];
p[i].enhour=stoi(p[i].temp);
p[i].temp="";
for(int j=l+13;j<=l+14;j++) p[i].temp+=a[j];
p[i].enm=stoi(p[i].temp);
p[i].temp="";
for(int j=l+16;j<=l+17;j++) p[i].temp+=a[j];
p[i].ens=stoi(p[i].temp);
p[i].temp="";
}
sort(p,p+n,cmp);
cout<<p[0].id<<" ";
sort(p,p+n,cmp1);
cout<<p[n-1].id<<endl;
return 0;
}