# 设计时间比较函数
# 字典存储每个人的信息
def t_bjw(s0, s1):  # 比较谁晚
    s0_s = int(s0[6:8])
    s1_s = int(s1[6:8])
    s0_m = int(s0[3:5])
    s1_m = int(s1[3:5])
    s0_h = int(s0[0:2])
    s1_h = int(s1[0:2])
    if s0_h > s1_h:
        return True
    elif s0_h < s1_h:
        return False
    elif s0_h == s1_h:
        if s0_m > s1_m:
            return True
        elif s0_m < s1_m:
            return False
        elif s0_m == s1_m:
            if s0_s > s1_s:
                return True
            elif s0_s < s1_s:
                return False
            elif s0_s == s1_s:
                return False


n = int(input())
zao = dict()
wan = dict()
for i in range(n):
    x = list(map(str, input().split(" ")))
    zao[x[1]] = x[0]
    wan[x[2]] = x[0]
ans_z = "24:00:00"
ans_w = "00:00:00"
for i in zao.items():
    if t_bjw(ans_z, i[0]):
        ans_z = i[0]
        ans0 = i[1]
for i in wan.items():
    if not t_bjw(ans_w, i[0]):
        ans_w = i[0]
        ans1 = i[1]
print(ans0, end=" ")
print(ans1)