#include <iostream>
#include <map>
using namespace std;

int main() {
    int n;
    cin >> n;
    string no, come, leave;
    map<string, string> intime, outtime;
    while(n --){
        cin >> no >> come >> leave;
        intime[come] = no;
        outtime[leave] = no;
    }
    auto iter = intime.begin();
    cout << iter->second << " ";
    iter = outtime.end();
    iter --;
    cout << iter->second << endl;

    return 0;
}