#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;


//习题10.5  开门人和关门人 
int main()
{
	int n;
	cin >> n;
	map<string, vector<string>> maps;
	string s;
	for (int i = 0; i < n; i++) {
		cin >> s;
		maps[s] = { "", "" };
		cin >> maps[s][0];
		cin >> maps[s][1];
	}
	string first = s, last = s, time = maps[s][0];
	for (auto it : maps) {
		if (it.second[0] < time) {
			time = it.second[0];
			first = it.first;
		}
	}
	time = maps[s][1];
	for (auto it : maps) {
		if (it.second[1] > time) {
			time = it.second[1];
			last = it.first;
		}
	}
	cout << first << " " << last << endl;

	return 0;
}