//土尔逊Torson 编写于2023/06/14
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<map>

using namespace std;

int main() {
	int n;
	string number, timein, timeout;
	while (scanf("%d", &n) != EOF) {
		map<string, string> First;
		map<string, string> Last;
		while (n--) {
			cin >> number >> timein >> timeout;
			First[timein] = number;
			Last[timeout] = number;
		}
		cout << (First.begin())->second << " " << (--Last.end())->second << endl;
	}
	system("pause");
	return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")