stoi方法,用于将字符串化为整数
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;


struct human {
	string cno;
	string time1;
	string time2;
};


bool compare1(human lhs, human rhs) {
	if (lhs.time1.substr(0, 2) == rhs.time1.substr(0, 2))
		return stoi(lhs.time1.substr(3, 2)) < stoi(rhs.time1.substr(3, 2));
	return lhs.time1.substr(0, 2) < rhs.time1.substr(0, 2);

}

bool compare2(human lhs, human rhs) {
	if (lhs.time2.substr(0, 2) == rhs.time2.substr(0, 2))
		return stoi(lhs.time2.substr(3, 2)) > stoi(rhs.time2.substr(3, 2));
	return lhs.time2.substr(0, 2) > rhs.time2.substr(0, 2);

}





int main() {
	int n;
	vector<human> vec;

	while (cin >> n) {
		vec.clear();
		for (int i = 0; i < n; i++)
		{
			human temp;
			cin >> temp.cno >> temp.time1 >> temp.time2;
			vec.push_back(temp);
		}
		sort(vec.begin(), vec.end(), compare1);
		cout << vec[0].cno << ' ';
		sort(vec.begin(), vec.end(), compare2);
		cout << vec[0].cno << endl;

	}

	

	return 0;
}