Vlad, like everyone else, loves to sleep very much.

Every day Vlad has to do $n$ things, each at a certain time. For each of these things, he has an alarm clock set, the $i$-th of them is triggered on $h_i$ hours $m_i$ minutes every day ($0 \le h_i < 24, 0 \le m_i < 60$). Vlad uses the $24$-hour time format, so after $h=12, m=59$ comes $h=13, m=0$ and after $h=23, m=59$ comes $h=0, m=0$.

This time Vlad went to bed at $H$ hours $M$ minutes ($0 \le H < 24, 0 \le M < 60$) and asks you to answer: how much he will be able to sleep until the next alarm clock.

If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length $0$.


-----Input-----

The first line of input data contains an integer $t$ ($1 \le t \le 100$) — the number of test cases in the test.

The first line of the case contains three integers $n$, $H$ and $M$ ($1 \le n \le 10, 0 \le H < 24, 0 \le M < 60$) — the number of alarms and the time Vlad went to bed.

The following $n$ lines contain two numbers each $h_i$ and $m_i$ ($0 \le h_i < 24, 0 \le m_i < 60$) — the time of the $i$ alarm. It is acceptable that two or more alarms will trigger at the same time.

Numbers describing time do not contain leading zeros.


-----Output-----

Output $t$ lines, each containing the answer to the corresponding test case. As an answer, output two numbers  — the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0.


-----Examples-----

Input
3
1 6 13
8 0
3 6 0
12 30
14 45
6 0
2 23 35
20 15
10 30
Output
1 47
0 0
10 55


-----Note-----

None

Difficulty: EASY

Solutions:
for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	l = [list(map(int, input().split())) for _ in range(n)]
	for i in range(n):
		l[i] = l[i][1] + l[i][0] * 60
	m += h * 60
	ans = 99999999
	for i in range(n):
		if l[i] >= m:
			ans = min(ans, l[i] - m)
		ans = min(ans, l[i] + 24 * 60 - m)
	print(ans // 60, ans % 60)


testcase = int(input())
for _ in range(testcase):
	(n, h, m) = [int(num) for num in input().split()]
	times = []
	for i in range(n):
		(a, b) = [int(num) for num in input().split()]
		times.append((a, b))
	c1 = (h, m)
	times.append(c1)
	times.sort()
	ind = times.index(c1)
	c2 = times[(ind + 1) % len(times)]
	if c2 < c1:
		c2 = (c2[0] + 24, c2[1])
	ans_min = (c2[0] - c1[0]) * 60 + c2[1] - c1[1]
	ans = (ans_min // 60, ans_min % 60)
	print(*ans)


for _ in range(int(input())):
	(t, h, m) = map(int, input().split())
	cvh = h * 60 + m
	l = []
	for i in range(t):
		(hi, mi) = map(int, input().split())
		cvi = hi * 60 + mi
		if cvi >= cvh:
			l.append(cvi - cvh)
		else:
			l.append(1440 + cvi - cvh)
	re = min(l)
	print(re // 60, re % 60)


for t in range(int(input())):
	(n, H, M) = map(int, input().split())
	time = H * 60 + M
	l = []
	for i in range(n):
		(h, m) = map(int, input().split())
		time_alarm = h * 60 + m
		if time_alarm < time:
			l.append(time_alarm - time + 24 * 60)
		else:
			l.append(time_alarm - time)
	time = min(l)
	print(time // 60, time % 60)


t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	minh = 24
	minh = 24
	for ii in range(n):
		(hh, mm) = map(int, input().split())
		if hh > H or (hh == H and mm >= M):
			if mm < M:
				(hh, mm) = (hh - H - 1, mm - M + 60)
			else:
				(hh, mm) = (hh - H, mm - M)
		elif mm < M:
			(hh, mm) = (hh - H - 1 + 24, mm - M + 60)
		else:
			(hh, mm) = (hh - H + 24, mm - M)
		if minh > hh or (minh == hh and minm >= mm):
			minh = hh
			minm = mm
	print(minh, minm)


m = int(input())
lst = []
for i in range(m):
	(n, H, M) = map(int, input().split())
	lst1 = [(n, H, M)]
	for j in range(n):
		lst1.append(list(map(int, input().split())))
	lst.append(lst1)
ans = []
for i in lst:
	(H, M) = (i[0][1], i[0][2])
	min_val = 24 * 60
	for j in i[1:]:
		if H < j[0]:
			if M > j[1]:
				min_val = min(min_val, (j[0] - 1 - H) * 60 + (60 + j[1] - M))
			else:
				min_val = min(min_val, (j[0] - H) * 60 + (j[1] - M))
		elif H > j[0]:
			exH = 23 - H
			exM = 60 - M
			exT = exH * 60 + exM
			min_val = min(min_val, exT + j[0] * 60 + j[1])
		elif M <= j[1]:
			min_val = min(min_val, j[1] - M)
		else:
			min_val = min(min_val, 24 * 60 - M + j[1])
	ans.append([min_val // 60, min_val % 60])
for i in ans:
	print(i[0], i[1])


a = int(input())
for x in range(a):
	(b, c, d) = map(int, input().split())
	o = c * 60 + d
	j = []
	for y in range(b):
		(e, f) = map(int, input().split())
		k = e * 60 + f
		if k >= o:
			j.append(k - o)
		else:
			j.append(23 * 60 + 60 - (o - k))
	mi = 1450
	for y in j:
		if y < mi:
			mi = y
	print(mi // 60, mi % 60)


t = int(input())
for _ in range(t):
	(n, H, M) = map(int, input().split())
	times = []
	for i in range(n):
		(h, m) = map(int, input().split())
		times.append((h, m))
	h_tot = 24
	m_tot = 60
	for (h, m) in times:
		c_h = (h - H - 1 + (60 - M + m) // 60) % 24
		c_m = (60 - M + m) % 60
		if h_tot > c_h:
			h_tot = c_h
			m_tot = c_m
		elif h_tot == c_h and m_tot > c_m:
			m_tot = c_m
	print(h_tot, m_tot)


for _ in range(int(input())):
	(n, s, m) = map(int, input().split())
	x = s * 60 + m
	a = []
	for i in range(n):
		(s, m) = map(int, input().split())
		a.append((s * 60 + m - x) % (24 * 60))
	ans = sorted(a)[0]
	print(ans // 60, ans % 60)


t = int(input())
finalArray = []
for i in range(t):
	[n, H, M] = [int(num) for num in input().split()]
	hourInMin = H * 60
	totalMin = hourInMin + M
	tempArray = []
	for j in range(n):
		[aH, aM] = [int(num) for num in input().split()]
		alarmHour = aH * 60
		totalAlarmMin = alarmHour + aM
		if totalMin > totalAlarmMin:
			calculatedMin = 24 * 60 + totalAlarmMin - totalMin
			tempArray.append(calculatedMin)
		else:
			calculatedMin = totalAlarmMin - totalMin
			tempArray.append(calculatedMin)
	minimum = min(tempArray)
	print(f'{minimum // 60} {minimum % 60}')
	finalArray.append(minimum)


def bedsorter(inp):
	(tasks, bedhour, bedmin) = inp.split()
	return (int(tasks), int(bedhour), int(bedmin))

def tasksorter(inp):
	(taskhour, taskmin) = inp.split()
	return (int(taskhour), int(taskmin))
t = int(input())
for i in range(t):
	(n, bedhour, bedmin) = bedsorter(input())
	timediffs = []
	for j in range(n):
		(taskhour, taskmin) = tasksorter(input())
		mindiff = (taskmin - bedmin) % 60
		if taskmin < bedmin:
			hourdiff = (taskhour - bedhour - 1) % 24
		else:
			hourdiff = (taskhour - bedhour) % 24
		totaldiff = hourdiff + mindiff / 60
		timediffs.append([totaldiff, hourdiff, mindiff])
	print(min(timediffs)[1], min(timediffs)[2])


def main():
	m = list(map(int, input().split()))
	number_tasks = m[0]
	h = m[1]
	mn = m[2]
	out_h = float('inf')
	out_m = float('inf')
	for i in range(number_tasks):
		task = list(map(int, input().split()))
		hour_task = task[0]
		min_task = task[1]
		if h <= hour_task:
			h2 = hour_task - h
		else:
			h2 = 24 - h + hour_task
		if mn > min_task:
			mn2 = 60 - mn + min_task
			if hour_task == h:
				h2 = 23
			else:
				h2 -= 1
		else:
			mn2 = min_task - mn
		if h2 == out_h and mn2 < out_m or h2 < out_h:
			out_h = h2
			out_m = mn2
	print(out_h, out_m)
tests = int(input())
for t in range(tests):
	main()


res = []
bums = int(input())
for i in range(bums):
	inpt = input().split()
	(n, h, m) = [int(x) for x in inpt]
	time_n = h + m / 60
	delta = []
	for j in range(n):
		inpt = input().split()
		(ht, mt) = [int(x) for x in inpt]
		time_till = ht + mt / 60
		if time_till < time_n:
			time_till += 24
		delta.append(time_till - time_n)
	delta = min(delta)
	hours = int(delta)
	mins_left = round((delta - hours) * 60)
	res.append((hours, mins_left))
for i in res:
	print(i[0], i[1])


for _ in range(int(input())):
	(n, h, m) = list(map(int, input().split()))
	dv = 24 * 60
	time = h * 60 + m
	ans = 1440
	for i in range(n):
		(h1, m1) = map(int, input().split())
		t1 = h1 * 60 + m1
		if t1 >= time:
			ans = min(ans, t1 - time)
		else:
			ans = min(ans, 1440 - time + t1)
	print(ans // 60, ans % 60)


t = int(input())
for p in range(t):
	(n, H, M) = map(int, input().split())
	ansh = 99
	ansm = 99
	ans = 99999999
	for i in range(n):
		(h, m) = map(int, input().split())
		if M > m:
			m += 60
			h -= 1
		if (h - H + 24) % 24 * 60 + m - M < ans:
			ans = (h - H + 24) % 24 * 60 + m - M
			ansh = (h - H + 24) % 24
			ansm = m - M
	print(ansh, ansm)


def main():
	t = 1
	t = int(input())
	for _ in range(t):
		(n, h, m) = map(int, input().split())
		nw = h * 60 + m
		ans = 10 ** 10
		for j in range(n):
			(hi, mi) = map(int, input().split())
			hl = hi * 60 + mi
			if hl < nw:
				hl += 24 * 60
			ans = min(ans, hl - nw)
		print(ans // 60, ans % 60)
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
mis = lambda : map(int, input().split())
INF = float('inf')

def f(now, alarm):
	if now > alarm:
		return alarm + (24 * 60 - now)
	return alarm - now

def solve():
	(n, h, m) = mis()
	time = h * 60 + m
	alarms = [list(mis()) for _ in range(n)]
	ans = INF
	for (p, q) in alarms:
		a_time = p * 60 + q
		k = f(time, a_time)
		ans = min(k, ans)
	print(*divmod(ans, 60))
T = int(input())
for _ in range(T):
	solve()


for i in range(int(input())):
	(n, h, m) = map(int, input().split())
	t = h * 60 + m
	ta = 10 ** 10
	for j in range(n):
		(h, m) = map(int, input().split())
		t1 = h * 60 + m
		if t1 < t:
			t1 += 24 * 60
		ta = min(t1, ta)
	print((ta - t) // 60, (ta - t) % 60)


import sys

def solve():
	inp = sys.stdin.readline
	(n, H, M) = map(int, inp().split())
	x = H * 60 + M
	r = 24 * 60 + 10
	for i in range(n):
		(h, m) = map(int, inp().split())
		y = h * 60 + m
		if y < x:
			r = min(r, y + 24 * 60 - x)
		else:
			r = min(r, y - x)
	print(r // 60, r % 60)

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


a = int(input())
for _ in range(a):
	(cl, hr, min) = [int(x) for x in input().split()]
	mintime = -1
	for i in range(cl):
		(hr1, min1) = [int(x) for x in input().split()]
		dif = (hr1 - hr) * 60 + (min1 - min)
		if dif < 0:
			dif = dif + 24 * 60
		if mintime == -1 or mintime > dif:
			mintime = dif
	print('%d %d' % (mintime / 60, mintime % 60))


t = int(input())
for _ in range(t):
	(n, h, m) = map(int, input().split())
	L = []
	for i in range(n):
		(a, b) = map(int, input().split())
		L.append((a, b))
	L.sort()
	ans = -1
	for i in range(n):
		if (h, m) > L[i]:
			pass
		else:
			ans = i
			break
	if ans == -1:
		minu = 24 * 60 - h * 60 - m + L[0][0] * 60 + L[0][1]
		print(minu // 60, minu % 60)
	else:
		minu = L[i][0] * 60 + L[i][1] - h * 60 - m
		print(minu // 60, minu % 60)


for _ in range(int(input())):
	(n, H, M) = map(int, input().split())
	min_time = 1440
	for _ in range(n):
		(h, m) = map(int, input().split())
		if (h * 60 + m - H * 60 - M) % 1440 < min_time:
			min_time = (h * 60 + m - H * 60 - M) % 1440
	print(min_time // 60, min_time % 60)


t = int(input())
for _ in range(t):
	(n, h, m) = [int(s) for s in input().split(' ')]
	curMins = h * 60 + m
	ans = float('inf')
	for i in range(n):
		(curH, curM) = [int(s) for s in input().split(' ')]
		mins = curH * 60 + curM
		diffM = (mins - curMins + 60 * 24) % (60 * 24)
		ans = min(ans, diffM)
	print(ans // 60, ans % 60)


for _ in range(int(input())):
	(n, H, M) = [int(x) for x in input().split()]
	l = []
	for i in range(n):
		(x, y) = [int(x) for x in input().split()]
		diff_x = x - H
		diff_y = y - M
		if diff_y < 0:
			diff_y = 60 + diff_y
			diff_x -= 1
		if diff_x < 0:
			diff_x += 24
		l.append((diff_x, diff_y))
	new_l = sorted(l, key=lambda x: x[0] * 60 + x[1])
	print(f'{new_l[0][0]} {new_l[0][1]}')


def time_diff(h1, m1, h2, m2):
	h = (h2 - h1) % 24
	m = (m2 - m1) % 60
	if m1 > m2:
		h = (h - 1) % 24
	return h * 60 + m
for _ in range(int(input())):
	(n, H, M) = map(int, input().split())
	A = 24 * 60
	for _ in range(n):
		(h, m) = map(int, input().split())
		A = min(A, time_diff(H, M, h, m))
	print(A // 60, A % 60)


t = int(input())
for i in range(t):
	(n, H, M) = list(map(int, input().split()))
	flag = 0
	need = H * 60 + M
	all = []
	for i in range(n):
		(h, m) = map(int, input().split())
		all.append(h * 60 + m)
	all = sorted(all)
	for elem in all:
		if elem >= need:
			print((elem - need) // 60, (elem - need) % 60)
			flag = 1
			break
	if flag == 0:
		print((24 * 60 - need + all[0]) // 60, (24 * 60 - need + all[0]) % 60)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	t = h * 60 + m
	a = []
	for i in range(n):
		(h, m) = map(int, input().split())
		b = h * 60 + m
		if b - t >= 0:
			a.append(b - t)
		else:
			a.append(1440 - t + b)
	print(min(a) // 60, min(a) % 60)


def ii():
	return int(input())

def il(sep=' '):
	if sep == '':
		return [int(i) for i in input()]
	return [int(i) for i in input().split(sep)]
t = ii()
for tt in range(t):
	(n, h, m) = il()
	sm = h * 60 + m
	ansm1 = 24 * 60
	ansm2 = 24 * 60
	for i in range(n):
		(h, m) = il()
		am = h * 60 + m
		if am < sm:
			ansm1 = min(ansm1, 24 * 60 - sm + am)
		else:
			ansm2 = min(ansm2, am - sm)
	ans = min(ansm1, ansm2)
	print(ans // 60, end=' ')
	print(ans % 60)


t = int(input())
for _ in range(t):
	(n, H, M) = map(int, input().split())
	minTime = (float('inf'), float('inf'))
	for __ in range(n):
		(h, m) = map(int, input().split())
		if (h, m) < (H, M):
			h += 24
		diff = 60 * (h - H) + m - M
		hh = diff // 60
		mm = diff % 60
		minTime = min(minTime, (hh, mm))
	print(*minTime)


import bisect

def inp_a():
	return [int(x) for x in input().split()]

def inp_s(k):
	arr = []
	for i in range(k):
		arr.append(input())
	return arr

def help():
	pass
for _ in range(int(input())):
	(n, h, m) = inp_a()
	alarms = []
	for i in range(n):
		(a, b) = inp_a()
		alarms.append(60 * a + b)
	alarms.sort()
	tm = 60 * h + m
	if tm in alarms:
		print(*[0, 0])
	else:
		idx = bisect.bisect_left(alarms, tm)
		if idx == len(alarms):
			time = 1440 - tm + alarms[0]
			arr = [time // 60, time % 60]
		else:
			time = alarms[idx] - tm
			arr = [time // 60, time % 60]
		print(*arr)


import sys, os
import timeit
from itertools import cycle, repeat, chain, islice

def count_sleep_time(start_hour, start_min, end_hour, end_min):
	start_time = start_hour * 60 + start_min
	end_time = end_hour * 60 + end_min
	if start_time > end_time:
		return 24 * 60 - start_time + end_time
	else:
		return end_time - start_time

def main():
	t = int(input())
	for _ in range(t):
		a = list(map(int, input().split()))
		res = sys.maxsize
		for i in range(a[0]):
			(h, m) = list(map(int, input().split()))
			res = min(res, count_sleep_time(a[1], a[2], h, m))
		print(res // 60, res % 60)
debug = False
start = timeit.default_timer()
if os.path.exists('input.txt'):
	debug = True
	sys.stdin = open('input.txt', 'r')
main()
if debug:
	print('Execution Time:', timeit.default_timer() - start)
	sys.exit()


for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	l = []
	for i in range(n):
		(a, b) = map(int, input().split())
		l.append(60 * a + b)
	hh = 25
	mm = 80
	q = 60 * h + m
	ans = 500000
	for i in range(n):
		if l[i] > q:
			ans = min(ans, l[i] - q)
		elif l[i] < q:
			ans = min(ans, 1440 - q + l[i])
		else:
			ans = 0
			break
	print(ans // 60, ans % 60)


def read_data():
	(n, H, M) = map(int, input().split())
	TIME = H * 60 + M
	min_time = 25 * 60
	for j in range(n):
		(h, m) = map(int, input().split())
		time = h * 60 + m
		if time - TIME < 0:
			time += 24 * 60
		if time - TIME < min_time:
			min_time = time - TIME
	return min_time
t = int(input())
for i in range(t):
	k = read_data()
	print(k // 60, k % 60)


for _ in range(int(input())):
	(n, H, M) = map(int, input().split())
	res = []
	(resh, resm) = (0, 0)
	for i in range(n):
		(h, m) = map(int, input().split())
		if m - M < 0:
			resm = 60 + m - M
			h -= 1
		else:
			resm = m - M
		if h - H < 0:
			resh = 24 + h - H
		else:
			resh = h - H
		res.append([resh, resm])
	print(*min(res))


n = int(input())
m = []
for el in range(n):
	(a, b, c) = map(int, input().split())
	t = b * 60 + c
	for i in range(a):
		(d, e) = map(int, input().split())
		p = d * 60 + e
		if p >= t:
			m.append(p - t)
		else:
			m.append(1440 - t + p)
	m.sort()
	print(m[0] // 60, m[0] % 60)
	m = []


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(n, h, m) = R()
	r = 1500
	for _ in [0] * n:
		(x, y) = R()
		r = min(r, (60 * (x - h) + y - m) % 1440)
	print(r // 60 % 24, r % 60)


t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	(min_h, min_m) = (24, 60)
	for i in range(n):
		(h, m) = map(int, input().split())
		(cur_dh, cur_dm) = (0, 0)
		if h < H:
			cur_dh = h + 24 - H
		elif h == H and m < M:
			cur_dh = h + 24 - H
		else:
			cur_dh = h - H
		if m < M:
			cur_dm = m + 60 - M
			cur_dh -= 1
		else:
			cur_dm = m - M
		if cur_dh * 60 + cur_dm < min_h * 60 + min_m:
			(min_h, min_m) = (cur_dh, cur_dm)
	print(min_h, min_m)


C = int(input())
for i in range(C):
	values = list(input().split())
	N = int(values[0])
	H = int(values[1])
	M = int(values[2])
	SLEEP = H * 60 + M
	ans = 1000000
	for _ in range(N):
		x = list(input().split())
		minutes = int(x[0]) * 60 + int(x[1])
		t = 0
		if minutes >= SLEEP:
			t = abs(minutes - SLEEP)
		else:
			t = 24 * 60 - SLEEP + minutes
		ans = min(ans, t)
	print('{hour} {minutes}'.format(hour=int(ans / 60), minutes=ans % 60))


import sys
input = sys.stdin.readline
t = int(input())
result = []
for _ in range(t):
	(n, hourOne, minuteOne) = map(int, input().split())
	array = []
	for i in range(n):
		(h, m) = map(int, input().split())
		array.append(h * 60 + m)

	def solve():
		(timeHours, timeMinutes) = (float('inf'), float('inf'))
		currentMinutes = hourOne * 60 + minuteOne
		for minutes in array:
			(hoursSleep, minuteSleep) = (None, None)
			if minutes >= currentMinutes:
				newMinutes = minutes - currentMinutes
				(hours, minutes) = (newMinutes // 60, newMinutes % 60)
				(hoursSleep, minutesSleep) = (hours, minutes)
			else:
				newMinutes = minutes + (1440 - currentMinutes)
				(hours, minutes) = (newMinutes // 60, newMinutes % 60)
				(hoursSleep, minutesSleep) = (hours, minutes)
			if hoursSleep < timeHours:
				(timeHours, timeMinutes) = (hoursSleep, minutesSleep)
			elif hoursSleep == timeHours and minutesSleep < timeMinutes:
				timeMinutes = minutesSleep
		return (timeHours, timeMinutes)
	(z, a) = solve()
	print(z, a)


for i in range(int(input())):
	(a, h1, m1) = list(map(int, input().split()))
	b = []
	current_time = h1 * 60 + m1
	for i in range(a):
		(h2, m2) = list(map(int, input().split()))
		up_time = h2 * 60 + m2
		if up_time < current_time:
			up_time += 60 * 24
		b.append(up_time - current_time)
	min_time = min(b)
	hours = min_time // 60
	min_time %= 60
	print(hours, min_time)


for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	x = h * 60 + m
	a = []
	for i in range(n):
		(H, M) = map(int, input().split())
		a.append(H * 60 + M)
	if x in a:
		a.sort()
		j = a.index(x)
		y = a[j]
		z = y - x
		s = z // 60
		r = z % 60
		t = [s, r]
		print(*t)
	else:
		a.append(x)
		a.sort()
		j = a.index(x)
		if j < n:
			y = a[j + 1]
			z = y - x
			s = z // 60
			r = z % 60
			t = [s, r]
			print(*t)
		elif j == n:
			y = a[0]
			z = abs(y - x + 1440)
			s = z // 60
			r = z % 60
			t = [s, r]
			print(*t)


t = int(input())
for _ in range(t):
	a = list(map(int, input().split()))
	(n, h, m) = (a[0], a[1], a[2])
	d = []
	for i in range(n):
		d.append(list(map(int, input().split())))
	d.sort()
	if [h, m] in d:
		print(*[0, 0])
	else:
		pt = -1
		for i in range(n):
			if d[i][0] > h or (d[i][0] == h and d[i][1] > m):
				pt = i
				break
		if pt >= 0:
			t = 60 * d[pt][0] + d[pt][1]
			l = t - 60 * h - m
			lf = [l // 60, l % 60]
			print(*lf)
		else:
			l = 24 * 60 - 60 * h - m + d[0][0] * 60 + d[0][1]
			lf = [l // 60, l % 60]
			print(*lf)


for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	e = 24 * 60
	x = h * 60 + m
	min_n = 10000000000000000
	for _ in range(n):
		(h_1, m_1) = map(int, input().split())
		x_1 = h_1 * 60 + m_1
		if x_1 - x < 0:
			r = x_1 - x + e
		else:
			r = x_1 - x
		min_n = min(r, min_n)
	if min_n >= 0:
		print(min_n // 60, min_n % 60)


t = int(input())
while t:
	t -= 1
	(n, H, M) = map(int, input().split(' '))
	Mi = H * 60 + M
	du = None
	while n:
		n -= 1
		(h, m) = map(int, input().split(' '))
		mi = h * 60 + m
		dur = mi - Mi if Mi <= mi else 24 * 60 - Mi + mi
		if du == None:
			du = dur
		elif dur < du:
			du = dur
	myH = du // 60
	myM = du % 60
	print('{} {}'.format(myH, myM))


t = int(input())
while t > 0:
	l = []
	t = t - 1
	mz = 100000
	mf = 0
	f = 1
	(n, H, M) = map(int, input().split())
	time = H * 60 + M
	while n > 0:
		n = n - 1
		(h, m) = map(int, input().split())
		l.append(h * 60 + m)
	for i in l:
		j = i - time
		if j == 0:
			f = 0
			print('0 0')
			break
		elif j > 0:
			if j < mz:
				mz = j
		elif j < 0:
			if j < mf:
				mf = j
	if mz != 100000 and f == 1:
		a = mz // 60
		b = mz % 60
		f = 0
		print(a, b)
	if mf != 0 and f == 1:
		mf = mf + 1440
		a = mf // 60
		b = mf % 60
		print(a, b)


t = int(input())
for i in range(t):
	(n, h, m) = list(map(int, input().split()))
	h_arr = []
	m_arr = []
	for j in range(n):
		(t1, t2) = list(map(int, input().split()))
		h_arr.append(t1)
		m_arr.append(t2)
	minim = 1440
	for j in range(n):
		d1 = h_arr[j] - h
		d2 = m_arr[j] - m
		if d1 < 0:
			d1 += 24
		if d2 < 0:
			if d1 > 0:
				d1 -= 1
				d2 += 60
			else:
				d1 = 23
				d2 += 60
		if minim > d1 * 60 + d2:
			minim = d1 * 60 + d2
	print(minim // 60, minim % 60)


for _ in range(int(input())):
	(a, b, c) = map(int, input().split())
	l = []
	for i in range(a):
		(x, y) = map(int, input().split())
		x -= 1
		y += 60
		r = x - b
		if r < 0:
			r += 24
		s = y - c
		r += s // 60
		s = s % 60
		r = r % 24
		l.append((r, s))
	(v, w) = min(l)
	print(v, w)


for _ in range(int(input())):
	l = list(map(int, input().split()))
	n = l[0]
	(h, m) = (l[1], l[2])
	alarms = []
	for i in range(n):
		(hi, mi) = map(int, input().split())
		ans = abs(h * 60 + m - (hi * 60 + mi))
		if hi < h:
			alarms.append(24 * 60 - ans)
		elif hi == h and mi < m:
			alarms.append(24 * 60 - ans)
		else:
			alarms.append(ans)
	res = min(alarms)
	resH = res // 60
	resM = res % 60
	print(resH, resM)


def reset(dh, dm):
	if dm >= 60:
		dm -= 60
		dh += 1
	elif dm < 0:
		dm += 60
		dh -= 1
	return (dh, dm)
t = int(input())
for _ in range(t):
	(n, H, M) = map(int, input().split())
	(rh, rm) = (24, 0)
	if H == 24:
		H = 0
	if M == 60:
		M = 0
	(H24, M24) = (23 - H, 60 - M)
	if M24 == 60:
		H24 += 1
		M24 = 0
	for i in range(n):
		(h, m) = map(int, input().split())
		if h < H:
			(dh, dm) = (H24 + h, M24 + m)
		elif h == H:
			if m < M:
				(dh, dm) = (H24 + h, M24 + m)
			elif m == M:
				(dh, dm) = (0, 0)
			else:
				(dh, dm) = (0, m - M)
		else:
			(dh, dm) = (h - H, m - M)
		(dh, dm) = reset(dh, dm)
		if dh < rh:
			(rh, rm) = (dh, dm)
		elif dh == rh:
			if dm < rm:
				rm = dm
	print(rh, rm)


a = int(input())
for i in range(a):
	(n, h, m) = map(int, input().split())
	t = h * 60 + m
	c = abs(t - 24 * 60)
	d = []
	for i in range(n):
		(h1, m1) = map(int, input().split())
		if h1 < h:
			d.append(c + h1 * 60 + m1)
		elif h1 == h and m1 < m:
			d.append(c + h1 * 60 + m1)
		else:
			d.append(-t + h1 * 60 + m1)
	print(min(d) // 60, min(d) % 60)


for x in range(int(input())):
	(t, hsleep, msleep) = input().split(' ')
	tsleep = int(hsleep) * 60 + int(msleep)
	tasks = []
	for x in range(int(t)):
		(temph, tempm) = input().split(' ')
		tempt = int(tempm) + int(temph) * 60
		if int(hsleep) > int(temph):
			tempt += 1440 - tsleep
		elif int(hsleep) < int(temph):
			tempt -= tsleep
		elif int(msleep) > int(tempm):
			tempt = 1440 - int(msleep) + int(tempm)
		elif int(msleep) < int(tempm):
			tempt -= tsleep
		else:
			tempt = 0
		tasks.append(tempt)
	tasks = sorted(tasks)
	closest = tasks[0]
	ht = int(closest / 60)
	closest = closest - 60 * ht
	print(ht, closest)


for i in range(int(input())):
	(n, h, m) = map(int, input().split())
	x = h * 60 + m
	a = []
	for j in range(n):
		(c, d) = map(int, input().split())
		z = c * 60 + d
		if z > x:
			a.append(z - x)
		elif z < x:
			a.append(z + (24 * 60 - x))
		else:
			a.append(0)
	a.sort()
	print(a[0] // 60, a[0] % 60)


t = int(input())
for a in range(t):
	nums = input().split()
	n = int(nums[0])
	H = int(nums[1])
	M = int(nums[2])
	Hours = []
	Minutes = []
	for i in range(n):
		nums = input().split()
		Hours.append(int(nums[0]))
		Minutes.append(int(nums[1]))
	for i in range(n):
		Hours.append(Hours[i] + 24)
		Minutes.append(Minutes[i])
	timefound = False
	totaltime = 60 * H + M
	mindifference = 1440
	for i in range(2 * n):
		currtime = 60 * Hours[i] + Minutes[i]
		if currtime >= totaltime and currtime - totaltime < mindifference:
			mindifference = currtime - totaltime
	M = mindifference % 60
	H = (mindifference - M) / 60
	print('%d %d' % (H, M))


t = int(input())
for i in range(t):
	(n, h, m) = map(int, input().split())
	l = []
	for i in range(n):
		(x, y) = map(int, input().split())
		l.append(x * 60 + y)
	tm = h * 60 + m
	st = 1440
	d = 0
	for i in range(n):
		if l[i] - tm < 0:
			d = l[i] - tm + 1440
		else:
			d = l[i] - tm
		st = min(d, st)
	H = st // 60
	M = st % 60
	print(str(H) + ' ' + str(M))


import math
t = int(input())
for _ in range(t):
	(n, h1, m1) = map(int, input().split())
	tot1 = h1 * 60 + m1
	arr = []
	for i in range(n):
		(h2, m2) = map(int, input().split())
		arr.append(h2 * 60 + m2)
	arr.sort()
	min_val = math.inf
	for i in range(n):
		if arr[i] >= tot1:
			min_val = min(arr[i], min_val)
	if min_val == math.inf:
		val = 24 * 60 + arr[0] - tot1
		print(val // 60, val % 60)
	else:
		val = min_val - tot1
		print(val // 60, val % 60)


def convert_to_scalar(hour, minutes):
	return int(hour * 60 + minutes)

def convert_to_time(value):
	minutes = value % 60
	return (int((value - minutes) / 60), int(minutes))

def check_next_alarm(bedTime, alarms):
	minimun_value = None
	maximum_value = None
	for time in alarms:
		if time >= bedTime:
			if maximum_value == None:
				maximum_value = time
			else:
				maximum_value = min(maximum_value, time)
		elif minimun_value == None:
			minimun_value = time
		else:
			minimun_value = min(minimun_value, time)
	if maximum_value == None:
		return convert_to_time(60 * 24 - bedTime + minimun_value)
	return convert_to_time(maximum_value - bedTime)
testcase = int(input())
res = []
while testcase > 0:
	alarms = []
	firstInput = input()
	firstInput = firstInput.split()
	alarmsSize = int(firstInput[0])
	bedtime = convert_to_scalar(int(firstInput[1]), int(firstInput[2]))
	while alarmsSize > 0:
		alarmTime = input()
		alarmTime = alarmTime.split(' ')
		alarmTime = convert_to_scalar(int(alarmTime[0]), int(alarmTime[1]))
		alarms.append(alarmTime)
		alarmsSize -= 1
	testcase -= 1
	(res_hour, res_minute) = check_next_alarm(bedtime, alarms)
	res.append((res_hour, res_minute))
for (hour, minutes) in res:
	print(hour, minutes)


testcase = int(input())
for _ in range(testcase):
	(n1, h1, m1) = input().split()
	n = int(n1)
	h = int(h1)
	m = int(m1)
	ah = 24
	am = 60
	for _ in range(n):
		(ha, ma) = input().split()
		hi = int(ha)
		mi = int(ma)
		if h <= hi:
			hd = hi - h
		else:
			hd = 24 - h + hi
		if m <= mi:
			md = mi - m
		else:
			md = 60 - m + mi
			if hd == 0:
				hd = 23
			else:
				hd -= 1
		if hd < ah:
			ah = hd
			am = md
		elif hd == ah:
			am = min(am, md)
	print(ah, am)


T = int(input())
for _ in range(T):
	(n, H, M) = map(int, input().split())
	currTime = H * 60 + M
	alarms = []
	for _ in range(n):
		(h, m) = map(int, input().split())
		alarms.append(h * 60 + m)
	alarms.sort()
	nextAlarm = 0
	for al in alarms:
		if al >= currTime:
			nextAlarm = al
			break
		if nextAlarm == 0:
			nextAlarm = alarms[0]
	if nextAlarm - currTime >= 0:
		diff = nextAlarm - currTime
	else:
		diff1 = 24 * 60 - currTime
		diff = diff1 + nextAlarm
	print(diff // 60, diff % 60)


t = int(input())
for _ in range(t):
	vlad = list(map(int, input().split()))
	lst = []
	for i in range(vlad[0]):
		templst = []
		templst.append(list(map(int, input().split())))
		lst.append(templst[0][0] * 60 + templst[0][1])
		templst.clear()
	sleeptime = vlad[1] * 60 + vlad[2]
	duration = 24 * 60
	for i in range(vlad[0]):
		if lst[i] < sleeptime:
			lst[i] += 24 * 60
		duration = min(duration, lst[i] - sleeptime)
	print(f'{duration // 60} {duration % 60}')


testcases = int(input())
for i in range(testcases):
	(n, H, M) = map(int, input().split())
	min_diff = 1439
	for j in range(n):
		(hours, minutes) = map(int, input().split())
		if hours > H or (hours == H and minutes >= M):
			time_diff = 60 * hours + minutes - (60 * H + M)
			min_diff = min(min_diff, time_diff)
		elif hours < H or (hours == H and minutes < M):
			time_diff = 1440 - 60 * H - M + (60 * hours + minutes)
			min_diff = min(min_diff, time_diff)
	print(str(min_diff // 60) + ' ' + str(min_diff % 60))


t = int(input())

class compare(list):

	def __lt__(lst1, lst2):
		if lst1[0] != lst2[0]:
			return lst1[0] < lst2[0]
		else:
			return lst1[1] < lst2[1]

def solve():
	(n, h, m) = map(int, input().split())
	sleep_time = h * 60 + m
	arr = []
	for i in range(n):
		(a, b) = map(int, input().split())
		alarm_time = a * 60 + b
		if alarm_time >= sleep_time:
			minutes = alarm_time - sleep_time
			if minutes < 60:
				arr += [[0, minutes]]
			else:
				arr += [[minutes // 60, minutes % 60]]
		else:
			minutes = 24 * 60 - (sleep_time - alarm_time)
			if minutes < 60:
				arr += [[0, minutes]]
			else:
				arr += [[minutes // 60, minutes % 60]]
	arr.sort(key=compare)
	print(arr[0][0], arr[0][1])
while t:
	solve()
	t -= 1


t = int(input())

def hm2time(h, m):
	h <<= 2
	h = (h << 4) - h
	return h + m
for i in range(t):
	nhm = input().split(' ')
	n = int(nhm[0])
	h = int(nhm[1])
	m = int(nhm[2])
	s = hm2time(h, m)
	mintime = 1440
	for j in range(n):
		hm = input().split(' ')
		time = hm2time(int(hm[0]), int(hm[1])) + 1440
		mintime = min(mintime, (time - s) % 1440)
	print(str(mintime // 60) + ' ' + str(mintime % 60))


def main():
	t = int(input())
	lim = 24 * 60
	for _ in range(t):
		(n, H, M) = [int(i) for i in input().split()]
		bed = H * 60 + M
		cur_min = lim
		state = False
		maxmin = lim
		for _ in range(n):
			(h, m) = [int(i) for i in input().split()]
			now = h * 60 + m
			if now < bed:
				state = True
			if now >= bed:
				maxmin = min(now, maxmin)
			cur_min = min(cur_min, now)
		if state:
			ans = maxmin - bed
			if maxmin == lim:
				ans += cur_min
		elif bed > cur_min:
			ans = lim - bed + cur_min
		else:
			ans = cur_min - bed
		(res_h, res_m) = (ans // 60, ans % 60)
		print(res_h, res_m)
	pass
main()


for _ in range(int(input())):
	(n, H, M) = map(int, input().split())
	ans = 999999
	const = H * 60 + M
	for i in range(n):
		(hi, mi) = map(int, input().split())
		t = hi * 60 + mi
		if t < const:
			t1 = (24 - H) * 60 - M
			t2 = hi * 60 + mi
			t = t1 + t2
			ans = min(ans, t)
		else:
			t = hi * 60 + mi
			ans = min(ans, abs(t - const))
	print(ans // 60, ans % 60)


for pratyush in range(int(input())):
	(n, h, m) = list(map(int, input().split()))
	k = float('inf')
	for q in range(n):
		(a, b) = list(map(int, input().split()))
		if a >= h:
			c = a - h
		else:
			c = 24 - h + a
		if b >= m:
			d = b - m
		else:
			if c != 0:
				c -= 1
			else:
				c = 23
			d = 60 - m + b
		e = c * 60 + d
		if e < k:
			(x, y) = (c, d)
			k = e
	print(x, y)


def diff(first, second):
	return (first[0] - second[0], first[1] - second[1])

def plus_day(time):
	return (time[0] + 24, time[1])

def to_norm(time):
	if time[1] < 0:
		return (time[0] - 1, time[1] + 60)
	return time
t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	sleep = (H, M)
	time = (23, 59)
	for j in range(n):
		alarm = tuple(map(int, input().split()))
		if alarm >= sleep:
			time = min(time, to_norm(diff(alarm, sleep)))
		else:
			time = min(time, to_norm(diff(plus_day(alarm), sleep)))
	print(*time)


t = int(input())
for o in range(t):
	(n, H, M) = map(int, input().split())
	L = []
	for k in range(n):
		(h, m) = map(int, input().split())
		if H <= h:
			hours = h - H
		else:
			hours = 24 - H + h
		if M <= m:
			minutes = m - M
		else:
			hours -= 1
			minutes = 60 - M + m
		if hours < 0:
			hours = 23
		L.append([hours, minutes])
	mnh = 30
	mnm = 100
	for i in range(len(L)):
		if mnh > L[i][0]:
			mnh = L[i][0]
			mnm = L[i][1]
		elif mnh == L[i][0]:
			if mnm > L[i][1]:
				mnm = L[i][1]
	print(mnh, mnm)


a = int(input())
for i in range(a):
	(b, c, d) = map(int, input().split())
	e = c * 60 + d
	arr = []
	for j in range(b):
		(h, m) = map(int, input().split())
		h = h * 60 + m
		arr.append(h)
	arr.sort()
	found = False
	t = 0
	for num in arr:
		if num >= e:
			t = num
			found = True
			break
	if found:
		f = t - e
		print(f // 60, f % 60)
	else:
		f = min(arr) - e + 24 * 60
		print(f // 60, f % 60)


for _ in range(int(input())):
	[n, H, M] = list(map(int, input().split()))
	C_H = []
	C_M = []
	clocks = []
	for i in range(n):
		[h, m] = list(map(int, input().split()))
		clocks.append([h, m])
		if h == H and m >= M:
			C_H.append(0)
			C_M.append(m - M)
		elif h == H and m < M:
			C_H.append(24)
			C_M.append(m - M)
		elif h > H:
			C_H.append(h - H)
			C_M.append(m - M)
		else:
			C_H.append(24 + h - H)
			C_M.append(m - M)
	d = []
	for i in range(n):
		x = 60 * C_H[i] + C_M[i]
		d.append(x)
	k = min(d) // 60
	l = min(d) - k * 60
	print(*[k, l])


for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	s = h * 60 + m
	lis = []
	for i in range(n):
		(a, b) = map(int, input().split())
		r = a * 60 + b - s
		if r < 0:
			r = 24 * 60 + r
		lis.append(r)
	print(min(lis) // 60, min(lis) % 60)


def sol(H, M, hm):
	min_diff = 60 * 24
	for (h, m) in hm:
		diff_h = h - H
		diff_m = m - M
		if diff_m < 0:
			diff_m += 60
			diff_h -= 1
		if diff_h < 0:
			diff_h += 24
		diff = 60 * diff_h + diff_m
		min_diff = min(diff, min_diff)
	res_h = min_diff // 60
	res_m = min_diff % 60
	print(res_h, res_m)
t = int(input())
for _ in range(t):
	(n, H, M) = [int(v) for v in input().split()]
	hm = [[int(v) for v in input().split()] for _ in range(n)]
	sol(H, M, hm)


t = int(input())
while t:
	(n, H, M) = list(map(int, input().split()))
	alarms = []
	for _ in range(n):
		alarms.append(list(map(int, input().split())))
	if [H, M] not in alarms:
		sleep = []
		for alarm in alarms:
			n_mins = 0
			n_hours = 0
			if M != alarm[1]:
				if M > alarm[1]:
					n_mins = 60 - M + alarm[1]
					if H != alarm[0]:
						n_hours = -1
					else:
						n_hours = 23
				else:
					n_mins = alarm[1] - M
			if H > alarm[0]:
				n_hours = n_hours + (24 - H) + alarm[0]
			else:
				n_hours = n_hours + alarm[0] - H
			sleep.append([n_hours, n_mins])
		sleep.sort()
		print(f'{sleep[0][0]} {sleep[0][1]}')
	else:
		print('0 0')
	t = t - 1


import sys

def get_string():
	return sys.stdin.readline().strip()

def get_array():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def get_vars():
	return map(int, sys.stdin.readline().split())

def get_num():
	return int(sys.stdin.readline())
tests = get_num()
for test in range(tests):
	(n, h, m) = get_vars()
	comb = []
	for i in range(n):
		(hours, mins) = get_vars()
		comb.append([hours, mins])
	comb.append([h, m])
	comb.sort(key=lambda x: (x[0], x[1]))
	i = 0
	while comb[i][0] != h or comb[i][1] != m:
		i += 1
	if i <= n - 1:
		next = comb[i + 1]
		curr = comb[i]
		if next[0] == curr[0]:
			print(0, next[1] - curr[1])
		else:
			x = 0
			x = 60 * (next[0] - curr[0]) + next[1] - curr[1]
			res_h = 0
			res_m = 0
			res_h = x // 60
			res_m = x % 60
			print(res_h, res_m)
	else:
		next = comb[0]
		curr = comb[-1]
		left_l = 60 * (24 - curr[0]) - curr[1]
		left_s = 60 * next[0] + next[1]
		x = left_l + left_s
		res_h = x // 60
		res_m = x % 60
		print(res_h, res_m)


for i in range(int(input())):
	a = list(map(int, input().split()))
	c = a[1] * 60 + a[-1]
	f = []
	for j in range(a[0]):
		b = list(map(int, input().split()))
		d = b[0] * 60 + b[-1]
		if d >= c:
			f.append(d - c)
		elif d < c:
			f.append(24 * 60 - c + d)
	print(min(f) // 60, min(f) % 60)


def sub(s, a):
	if a[1] >= s[1]:
		mins = a[1] - s[1]
	else:
		a[1] += 60
		a[0] -= 1
		mins = a[1] - s[1]
	if a[0] >= s[0]:
		hours = a[0] - s[0]
	else:
		hours = 24 - s[0]
		hours = a[0] + hours
	return [hours, mins]
t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	alarms = []
	for k in range(n):
		(h, m) = map(int, input().split())
		alarms.append([h, m])
	minTimeuu = 999999999999
	minArr = []
	for i in alarms:
		sleepTime = sub([H, M], i)
		minsTime = sleepTime[0] * 60 + sleepTime[1]
		if minsTime < minTimeuu:
			minTimeuu = minsTime
			minArr = sleepTime
	print(str(minArr[0]) + ' ' + str(minArr[1]))


for _ in range(int(input())):
	(n, h, m) = map(int, input().split())
	ct = h * 60 + m
	delta = float('inf')
	for _ in range(n):
		(h, m) = map(int, input().split())
		ctd = h * 60 + m
		if ctd >= ct:
			delta = min(delta, ctd - ct)
		else:
			delta = min(delta, 24 * 60 + ctd - ct)
	print(delta // 60, delta % 60)


def solve():
	(n, h, m) = map(int, input().split())
	count = []
	for i in range(n):
		(h1, m1) = map(int, input().split())
		if h == h1 and m == m1:
			count.append(0)
		else:
			t1 = h * 60 + m
			t2 = h1 * 60 + m1
			if t2 > t1:
				count.append(t2 - t1)
			else:
				t = 1440 - t1 + t2
				count.append(t)
	c = min(count)
	h = int(c / 60)
	m = c % 60
	print(str(h) + ' ' + str(m))
t = int(input())
for i in range(t):
	solve()


def Solve():
	for t in range(int(input())):
		(n, H, M) = map(int, input().split())
		s = H * 60 + M
		times = []
		for _ in range(n):
			(h, m) = map(int, input().split())
			times.append(h * 60 + m)
		if s in times:
			print(0, 0)
		else:
			times.append(s)
			times.sort()
			if times[-1] == s:
				i = times[0]
				sleep = 24 * 60 - s + i
				print(sleep // 60, sleep % 60)
			else:
				i = times[times.index(s) + 1]
				sleep = i - s
				print(sleep // 60, sleep % 60)
Solve()


t = int(input())
for eh in range(t):
	(n, hh, mm) = list(map(int, input().split(' ')))
	cur = hh * 60 + mm
	ans = []
	for i in range(n):
		(h, m) = list(map(int, input().split(' ')))
		val = h * 60 + m - cur
		if val < 0:
			val += 24 * 60
		ans += [val]
	yes = min(ans)
	print(yes // 60, yes % 60)


t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	X = H * 60 + M
	mini = 10 ** 18
	for j in range(n):
		(h, m) = map(int, input().split())
		x = h * 60 + m
		if x < X:
			x = 24 * 60 - X + x
		else:
			x = x - X
		if x < mini:
			mini = x
	print(mini // 60, mini % 60)


t = int(input())
for i in range(t):
	(n, sleeph, sleepm) = list(map(int, input().split()))
	(mini_timeh, minitimem) = (0, 0)
	ans = float('inf')
	for i in range(n):
		(alarmh, alarmm) = list(map(int, input().split()))
		if alarmh < sleeph:
			alarmh += 24
		if alarmh == sleeph:
			if alarmm < sleepm:
				alarmh += 24
		currenth = alarmh - sleeph
		currentm = alarmm - sleepm
		if currentm < 0:
			currenth -= 1
			currentm = 60 + currentm
		if currenth * 60 + currentm < ans:
			ans = currenth * 60 + currentm
			(mini_timeh, minitimem) = (currenth, currentm)
	print(mini_timeh, minitimem)


import math

def func(sleep_time, alarms):
	wake_time = [24, 60]
	for alarm in alarms:
		hrs = alarm[0] - sleep_time[0]
		min = alarm[1] - sleep_time[1]
		if min < 0:
			min = 60 + min
			hrs -= 1
		if hrs < 0:
			hrs += 24
		if wake_time[0] > hrs:
			wake_time[0] = hrs
			wake_time[1] = min
		elif wake_time[0] == hrs:
			if wake_time[1] > min:
				wake_time[0] = hrs
				wake_time[1] = min
	return wake_time
t = int(input())
while t:
	(n, H, M) = list(map(int, input('').split()))
	alarms = []
	for i in range(n):
		alarm = list(map(int, input('').split()))
		alarms.append(alarm)
	(hours, minutes) = func([H, M], alarms)
	print(hours, minutes)
	t -= 1


t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	arr = []
	num_sleep = 60 * H + M
	min_time = 10 ** 9
	for j in range(n):
		(h, m) = map(int, input().split())
		num_bud = 60 * h + m
		if num_bud - num_sleep < 0:
			count = 24 * 60 - (num_sleep - num_bud)
		else:
			count = num_bud - num_sleep
		min_time = min(min_time, count)
	min_h = min_time // 60
	min_m = min_time - min_h * 60
	print(min_h, min_m)


for i in range(int(input())):
	(n, h, m) = map(int, input().split())
	total = h * 60 + m
	l = []
	for i in range(n):
		l.append(list(map(int, input().split())))
	l1 = []
	j = 0
	for i in l:
		time = i[0] * 60 + i[1]
		if total <= time:
			l1.append(time - total)
		else:
			l1.append(24 * 60 - total + time)
	m = min(l1)
	print(m // 60, m % 60)


import math
for i in range(int(input())):
	(a, b, c) = map(int, input().split())
	k = b * 60 + c
	h = []
	m = []
	for i in range(a):
		(z, x) = map(int, input().split())
		h.append(z)
		m.append(x)
	O = 1440
	for i in range(a):
		H = h[i] * 60 + m[i]
		if H < k:
			f = 1440 - k + H
			if f < O:
				O = f
		else:
			f = H - k
			if f < O:
				O = f
	B = math.floor(O / 60)
	C = O % 60
	print(B, C)


for _ in range(int(input())):
	l = []
	(n, hour, minn) = map(int, input().split())
	for i in range(n):
		l.append([int(x) for x in input().split()])
	l.sort()
	(h, m) = (l[0][0], l[0][1])
	l.append([23, 59])
	flag = False
	for i in range(n + 1):
		h = l[i][0]
		m = l[i][1]
		if hour == h and minn <= m or hour < h:
			ansh = h - hour
			if minn > m:
				ansm = 60 - (minn - m)
				ansh -= 1
			else:
				ansm = m - minn
			break
	if i < n:
		print(ansh, ansm)
	else:
		h = 23
		m = 60
		if hour == h and minn <= m or hour < h:
			ansh = h - hour
			if minn > m:
				ansm = 60 - (minn - m)
				ansh -= 1
			else:
				ansm = m - minn
		ansh = ansh + l[0][0]
		ansm = ansm + l[0][1]
		ansh = ansh + ansm // 60
		ansm = ansm % 60
		print(ansh, ansm)


t = int(input())
for _ in range(t):
	(n, h, m) = map(int, input().split())
	m += h * 60
	a = [0] * n
	for i in range(n):
		(h1, m1) = map(int, input().split())
		m1 += h1 * 60
		if m1 >= m:
			a[i] = m1 - m
		else:
			a[i] = m1 + (1440 - m)
	m1 = min(a)
	print(m1 // 60, m1 % 60)


t = input()
ans = []
for i in range(int(t)):
	(n, H, M) = list(map(int, input().split()))
	ref_minutes = H * 60 + M
	arr = []
	for j in range(n):
		(h, m) = list(map(int, input().split()))
		minutes = h * 60 + m
		arr.append(minutes)
	arr.sort()
	arr += [24 * 60 + x for x in arr]
	arr_today = []
	for j in range(0, 2 * n):
		if arr[j] >= ref_minutes:
			arr_today.append(arr[j])
	sleep = arr_today[0] - ref_minutes
	sleep_hour = sleep // 60 % 24
	sleep_min = sleep % 60
	print(sleep_hour, sleep_min)


t = int(input())
for _ in range(0, t):
	inputs = [int(num) for num in input().split()]
	n = inputs[0]
	h = inputs[1]
	m = inputs[2]
	if h == 0:
		h = 24
	if m == 0:
		m = 60
		h = h - 1
	minh = 25
	minm = 61
	diffm = 24 * 60 + 10
	mins1 = h * 60 + m
	for i in range(0, n):
		inputs1 = [int(num) for num in input().split()]
		hi = inputs1[0]
		mi = inputs1[1]
		if hi == 0:
			hi = 24
		if mi == 0:
			mi = 60
			hi = hi - 1
		mins2 = hi * 60 + mi
		if mins2 >= mins1:
			diff = mins2 - mins1
		else:
			diff = 24 * 60 - mins1 + mins2
		if diff < diffm:
			diffm = diff
	hreq = diffm // 60
	mreq = diffm % 60
	print(f'{hreq} {mreq}')


t = int(input())
for i in range(t):
	(n, hs, ms) = map(int, input().split())
	slp = hs * 60 + ms
	mn = 23 * 60 + 59
	for j in range(n):
		(h, m) = map(int, input().split())
		wkp = h * 60 + m
		if slp <= wkp:
			mn = min(mn, wkp - slp)
		else:
			mn = min(mn, 24 * 60 - slp + wkp)
	print(mn // 60, mn % 60)


tc = int(input())
while tc > 0:
	tc -= 1
	time1 = list()
	(n, H, M) = map(int, input().split())
	N = n
	while n > 0:
		n -= 1
		(h, m) = map(int, input().split())
		t = h * 60 + m
		time1.append(t)
	my = H * 60 + M
	ans = 10000
	for i in range(N):
		aaa = time1[i]
		if my > aaa:
			temp = 1440 - my + aaa
			if temp < ans:
				ans = temp
		else:
			temp = aaa - my
			if temp <= ans:
				ans = temp
	(xx, yy) = (ans // 60, ans % 60)
	print(xx, yy)


def fun():
	t = int(input())
	while t:
		(n, H, M) = [int(i) for i in input().split()]
		gvn = H * 60 + M
		mni = 60000
		while n:
			(hi, mi) = [int(i) for i in input().split()]
			mn = hi * 60 + mi
			if mn - gvn >= 0:
				mni = min(mn - gvn, mni)
			elif mn - gvn < 0:
				mni = min(mn - gvn + 1440, mni)
			n -= 1
		print(mni // 60, mni % 60)
		t -= 1
fun()


t = int(input())
for i in range(t):
	(n, H, M) = map(int, input().split())
	arr = [[*map(int, input().split())] for _ in range(n)]
	(hour, minuts) = (float('inf'), float('inf'))
	for x in arr:
		if x[0] < H or (x[0] == H and x[1] < M):
			if M > 0:
				(t1, t2) = (23 - H + x[0], 60 - M + x[1])
			else:
				(t1, t2) = (24 - H + x[0], x[1])
			if t2 >= 60:
				t1 += 1
				t2 = t2 - 60
			if hour > t1:
				hour = t1
				minuts = t2
			if hour == t1:
				if minuts > t2:
					minuts = t2
		else:
			(t1, t2) = (x[0] - H, x[1] - M)
			if t2 < 0:
				t1 -= 1
				t2 = 60 + t2
			if hour > t1:
				hour = t1
				minuts = t2
			if hour == t1:
				if minuts > t2:
					minuts = t2
	print(hour, minuts)


for _ in range(int(input())):
	(n, H, M) = map(int, input().split())
	s = set()
	for _ in range(n):
		(h, m) = map(int, input().split())
		s.add((h, m))
	a = (H * 60 + M) % 1439
	b = a
	while True:
		if (a // 60, a % 60) in s:
			print((a - b) // 60 if (a - b) // 60 >= 0 else 24 + (a - b) // 60, (a - b) % 60)
			break
		else:
			a += 1
			if a == 1440:
				a = 0


for _ in range(int(input())):
	(n, H, M) = map(int, input().split())
	T = H * 60 + M
	res = float('inf')
	for i in range(n):
		(h, m) = map(int, input().split())
		t = h * 60 + m
		if t >= T:
			res = min(res, t - T)
		else:
			res = min(res, 24 * 60 - T + t)
	print(res // 60, res % 60)


t = int(input())
for i in range(t):
	(n, h, m) = map(int, input().split())
	a = []
	for j in range(n):
		(h1, m1) = map(int, input().split())
		a.append((h1, m1))
	result = []
	for k in range(n):
		m2 = a[k][1]
		h2 = a[k][0]
		h_temp = h
		if m2 >= m:
			m2 -= m
		else:
			m2 += 60
			m2 -= m
			h_temp += 1
		if h2 >= h_temp:
			h2 -= h_temp
		else:
			h2 += 24
			h2 -= h_temp
		result.append((h2, m2))
	m = min(result)
	print(m[0], m[1])


a = int(input())
b = []
c = []
for i in range(a):
	b.append(list(map(int, input().split())))
	c.append([])
	for j in range(b[i][0]):
		c[i].append(list(map(int, input().split())))
d = []
for s in b:
	d.append(s[1] * 60 + s[2])
for e in range(len(c)):
	for f in range(len(c[e])):
		h = c[e][f][0] * 60 + c[e][f][1]
		c[e][f] = []
		c[e][f].append(h)
for e in range(len(c)):
	for f in range(len(c[e])):
		c[e][f] = c[e][f][0]
k = []
for e in range(len(c)):
	if max(c[e]) >= d[e]:
		while min(c[e]) < d[e]:
			for l in c[e]:
				if l < d[e]:
					c[e].remove(l)
for m in range(len(c)):
	if min(c[m]) >= d[m]:
		print(abs(min(c[m]) - d[m]) // 60, abs(min(c[m]) - d[m]) % 60)
	elif min(c[m]) < d[m]:
		print((1440 - d[m] + min(c[m])) // 60, (1440 - d[m] + min(c[m])) % 60)