#include <bits/stdc++.h>
using namespace std;
struct stu {
string n;
int d, m, y;
};
bool com(const struct stu& x, const struct stu& y) {
if (x.y < y.y) {
return true;
}
else if (x.y == y.y) {
if (x.m < y.m) {
return true;
}
else if (x.m == y.m) {
if (x.d < y.d) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int k;
cin >> k;
vector<stu> arr;
for (int i = 0; i < k; i++) {
string s;
int a, b, c;
cin >> s >> a >> b >> c;
arr.push_back({ s,a,b,c });
}
sort(arr.begin(), arr.end(), com);
cout << arr[k - 1].n << "\n" << arr[0].n;
return 0;
}