#include <bits/stdc++.h>
using namespace std;
class person{
    public:
    int inTime;
    int outTime;
    string ID;
};
int toTime(string s){
    for(auto it = s.begin();it!=s.end();){
        if(*it==':')it=s.erase(it);
        else it++;
    }
    //无冒号的时间转换为int
    return stoi(s);
}
int main() {
    int n;
    while(cin>>n){
        person arr[n];
        for(int i =0;i<n;i++){
            string id,in,out;
            cin>>id>>in>>out;
            person p;
            p.ID = id;
            p.inTime = toTime(in);
            p.outTime = toTime(out);
            arr[i] = p;
        }
        int openTime=INT_MAX,closeTime=-1;
        int openIndex=0,closeIndex=0;
        for(int i =0;i<n;i++){
            if(arr[i].inTime<openTime){
                openTime = arr[i].inTime;
                openIndex = i;
            }
            if(arr[i].outTime>closeTime){
                closeTime = arr[i].outTime;
                closeIndex = i;
            }
        }
        cout<<arr[openIndex].ID<<" "<<arr[closeIndex].ID<<endl;
    }
}
// 64 位输出请用 printf("%lld")