#include <iostream>
#include <vector>
#include <map>

#define int long long

using namespace std;


class Solver {

};

signed main() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0)break;

		map<int, int> vals;
		for (int i = 0; i < n; i++) {
			int id, m1, s1, m2, s2, m3, s3, m4, s4;
			cin >> id >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
			int val = m1 * 60 + s1 + m2 * 60 + s2 + m3 * 60 + s3 + m4 * 60 + s4;
			vals.emplace(val,id);
		}

		int itr = 0;
		for (auto val : vals) {
			if (itr == 0 || itr == 1 || itr == vals.size() - 2) {
				cout << val.second << endl;
			}
			itr++;
		}
	}

}
