#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class CA
{
public:
enum{N=16,TIME=9};
void run();
private:
struct member
{
char idname[N];
char intime[TIME];
char outtime[TIME];
member *next;
};
int m;
member *men;
};
void CA::run()
{
cin>>m;
int i;
men=NULL;
member *tmp;
for(i=0;i<m;i++)
{
tmp=new member;
scanf("%s%s%s",tmp->idname,tmp->intime,tmp->outtime);
tmp->next=men;
men=tmp;
}
if(men==NULL)
return;
member *minin,*maxout;
tmp=men;
minin=men;
maxout=men;
tmp=men->next;
while(tmp!=NULL)
{
if(strcmp(minin->intime,tmp->intime)>0)
minin=tmp;
if(strcmp(maxout->outtime,tmp->outtime)<0)
maxout=tmp;
men=tmp;
tmp=men->next;
}
printf("%s %s",minin->idname,maxout->idname);
}
int main()
{
CA *a=new CA;
a->run();
return 0;
}