//map的底层是用红黑树实现的,将元素按照关键字进行排序
//然后这题就可以用map秒掉了
#include <iostream>
#include<map>
using namespace std;

int main() {
    int m;
    while (cin >>m) { 
        map<string,string> mymap1;
        map<string,string> mymap2;
        for(int i=1;i<=m;i++)
        {
            string id,begin,end;
            cin>>id;cin>>begin;cin>>end;
            mymap1[begin]=id;
            mymap2[end]=id;
        }
        map<string,string>::iterator it1;
        map<string,string>::iterator it2;
        it1=mymap1.begin();
        it2=mymap2.end();it2--;
        cout<<it1->second<<" "<<it2->second<<endl;
    }
}