#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
/*
小结:题目比较简单,主要是两个sort有点蠢
可以用两个变量保存最早的时间和对应的index,用两个变量保存最晚的时间和对应的index
*/
struct Person {
string name;
int come;
int leave;
}person[100005];
int time(int h,int m,int s) {
return h * 3600 + m * 60 + s;
}
bool cmp1(Person p1, Person p2) {
return p1.come < p2.come;
}
bool cmp2(Person p1, Person p2) {
return p1.leave > p2.leave;
}
int N;
int main() {
cin >> N;
int hh, mm, ss;
for (int i = 0; i < N; i++) {
cin >> person[i].name;
scanf("%d:%d:%d", &hh, &mm, &ss);
person[i].come = time(hh, mm, ss);
scanf("%d:%d:%d", &hh, &mm, &ss);
person[i].leave = time(hh, mm, ss);
}
sort(person, person + N, cmp1);
cout<<person[0].name<<" ";
sort(person, person + N, cmp2);
cout << person[0].name;
system("pause");
return 0;
}