"""
2
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11
"""

N = int(input())
for _ in range(N):
    M = int(input())
    intervals = []
    for _ in range(M):
        line = input().split(" ")
        h,a,b,c,d = line
        dist = abs(int(a) - int(b)) + abs(int(c) - int(d)) 
        depart = int(h[:1]) * 60 + int(h[3:])
        intervals.append((depart,depart+dist))
    intervals.sort()
    print(intervals)