#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main() {
int n;
string lock, unlock,id;
int min_in=24*60*60, max_out=-1;
cin >> n;
while (n--) {
string s;
cin >> s;
int h1, m1, s1, h2, m2, s2;
scanf("%d:%d:%d %d:%d:%d", &h1, &m1, &s1, &h2, &m2, &s2);
int clock1 = h1 * 60 * 60 + m1 * 60 + s1;
int clock2 = h2 * 60 * 60 + m2 * 60 + s2;
if (min_in > clock1) {
min_in = clock1;
unlock = s;
}
if (max_out < clock2) {
max_out = clock2;
lock = s;
}
}
cout << unlock << " " << lock << endl;
return 0;
}