#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 110;
int main(){
int n, x1, x2, x3, y1, y2, y3;
int x, y, tin = 235959, tout = 0;
scanf("%d", &n);
string str, in, out;
for(int i = 0; i < n; i++){
cin >> str;
scanf("%d:%d:%d %d:%d:%d", &x1, &x2, &x3, &y1, &y2, &y3);
x = x1*10000 + x2*100 + x3;
y = y1*10000 + y2*100 + y3;
if(tin > x){tin = x; in = str;}
if(tout < y){tout = y; out = str;}
}
cout << in << " " << out << endl;
}