#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
string open_time,close_time,open_id,close_id;
for(int i=0;i<n;i++)
{
string in_time,out_time,id;
cin>>id>>in_time>>out_time;
if(in_time<open_time||open_time.empty())
{
open_time=in_time;
open_id=id;
}
if(out_time>close_time||close_time.empty())
{
close_time=out_time;
close_id=id;
}
}
cout<<open_id<<' '<<close_id<<endl;
return 0;
}