#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
    int n;
    while(cin>>n){
        getchar();//eat'\n'
        map<string,string> rig;
        vector<string> at;//arrive time
        vector<string> lt;//leave time
        for(int i=0;i<n;i++){
            string x;
            getline(cin, x);
            istringstream record(x);
            string ID,arriveT,leaveT;
            record>>ID>>arriveT>>leaveT;
            rig[leaveT]=ID;
            rig[arriveT]=ID;
            lt.push_back(leaveT);
            at.push_back(arriveT);
        }
        sort(lt.begin(),lt.end());
        sort(at.begin(),at.end());
        cout<<rig[at.front()]<<" "<<rig[lt.back()]<<endl;
    }
}