//
// Created by yang on 2023/5/23.
//
#include "cstdio"
#include "string"

using namespace std;

// substr [)
struct Info {
    string id;
    string s_time;
    string e_time;
};

int main() {
    int M;
    scanf("%d", &M);
    getchar();  // 吃掉回车
    string start_id, start_t = "23:59:59", end_id, end_t = "00:00:00";
    for (int i = 0; i < M; ++i) {
		  // 读取的时候顺带比较即可
        char line[100];
        fgets(line, sizeof line, stdin);
        string line_str = line;
        line_str.pop_back();
        string id = line_str.substr(0, line_str.find(" "));
        string time = line_str.substr(line_str.find(" ") + 1);
        string s_time = time.substr(0, time.find(" "));
        string e_time = time.substr(time.find(" ") + 1);
//        char id[16], s_time[8], e_time[8];
//        scanf("%s", id); getchar();
//        scanf("%s", s_time); getchar();
//        scanf("%s", e_time); getchar();

        if (s_time < start_t) {
            start_id = id;
            start_t = s_time;
        }
        if (e_time > end_t) {
            end_id = id;
            end_t = e_time;
        }
//        fgets(id, sizeof id, stdin);
//        printf("%s", id);
    }
    printf("%s %s\n", start_id.c_str(), end_id.c_str());
    return 0;
}