#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
int main(){
int n;
scanf("%d", &n);
string str, str1, str2;
int hh1, mm1, ss1, hh2, mm2, ss2, min = 999999, max = -1;
for(int i = 0; i < n; i++){
int temp1, temp2;
cin >> str;
scanf("%d:%d:%d", &hh1, &mm1, &ss1);
temp1 = hh1 * 3600 + mm1 * 60 + ss1;
if(min > temp1){
str1 = str;
min = temp1;
}
scanf("%d:%d:%d", &hh2, &mm2, &ss2);
temp2 = hh2 * 3600 + mm2 * 60 + ss2;
if(max < temp2){
str2 = str;
max = temp2;
}
}
cout << str1 << " " << str2;
}