#include <bits/stdc++.h>
using namespace std;
int main()
{
    string open_time = "99:99:99", close_time, open_id, close_id;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string id, in_time, out_time;
        cin >> id >> in_time >> out_time;
        if (in_time < open_time)
        {
            open_id = id;
            open_time = in_time;
        }
        if (out_time > close_time)
        {
            close_time = out_time;
            close_id = id;
        }
    }
    cout << open_id << ' ' << close_id << endl;
    return 0;
}
