There is a prison that can be represented as a rectangular matrix with $n$ rows and $m$ columns. Therefore, there are $n \cdot m$ prison cells. There are also $n \cdot m$ prisoners, one in each prison cell. Let's denote the cell in the $i$-th row and the $j$-th column as $(i, j)$.

There's a secret tunnel in the cell $(r, c)$, that the prisoners will use to escape! However, to avoid the risk of getting caught, they will escape at night.

Before the night, every prisoner is in his own cell. When night comes, they can start moving to adjacent cells. Formally, in one second, a prisoner located in cell $(i, j)$ can move to cells $( i - 1 , j )$ , $( i + 1 , j )$ , $( i , j - 1 )$ , or $( i , j + 1 )$, as long as the target cell is inside the prison. They can also choose to stay in cell $(i, j)$.

The prisoners want to know the minimum number of seconds needed so that every prisoner can arrive to cell $( r , c )$ if they move optimally. Note that there can be any number of prisoners in the same cell at the same time.


-----Input-----

The first line contains an integer $t$ $(1 \le t \le 10^4)$, the number of test cases.

Each of the next $t$ lines contains four space-separated integers $n$, $m$, $r$, $c$ ($1 \le r \le n \le 10^9$, $1 \le c \le m \le 10^9$).


-----Output-----

Print $t$ lines, the answers for each test case.


-----Examples-----

Input
3
10 10 1 1
3 5 2 4
10 2 5 1
Output
18
4
6


-----Note-----

None

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Complete search


Solutions:
for _ in [*open(0)][1:]:
	(n, m, r, c) = map(int, _.split())
	print(max(r + c - 2, n + m - r - c, m + r - c - 1, n + c - r - 1))


for s in [*open(0)][1:]:
	(n, m, r, c) = map(int, s.split())
	print(max(m - c, c - 1) + max(r - 1, n - r))


result = []
for t in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	t = 0
	t = c - 1 if c - 1 > m - c else m - c
	t += r - 1 if r - 1 > n - r else n - r
	result.append(t)
for i in result:
	print(i)


import sys
input = sys.stdin.readline

def solve():
	(n, m, r, c) = map(int, input().split())
	return max(r - 1 + c - 1, n - r + c - 1, n - r + m - c, r - 1 + m - c)
for _ in range(int(input())):
	print(solve())


table = []
while True:
	try:
		l = input()
	except EOFError:
		break
	table.append(l)
tries = int(table[0])
for i in range(1, tries + 1):
	line = table[i].split()
	matrixLength1 = int(line[0])
	matrixLength2 = int(line[1])
	coord1 = int(line[2])
	coord2 = int(line[3])
	leftLength = coord1 - 1
	rightLength = matrixLength1 - coord1
	if leftLength > rightLength:
		max = leftLength
	else:
		max = rightLength
	upperLength = coord2 - 1
	lowerLength = matrixLength2 - coord2
	if upperLength > lowerLength:
		high = upperLength
	else:
		high = lowerLength
	steps = max + high
	print(steps)


import sys
from collections import defaultdict, Counter as CC
from math import sqrt, ceil, floor, log2
INF = float('inf')
MOD = 10 ** 9 + 7
read = lambda : [int(s) for s in sys.stdin.readline().split()]

def solve():
	(n, m, r, c) = read()
	res = max(r - 1, n - r) + max(c - 1, m - c)
	print(res)
_ = 1
_ = int(input())
while _:
	solve()
	_ -= 1


from re import X
n = int(input())
s = []
for i in range(n):
	x = list(map(int, input().split()))
	(n, m, r, c) = x
	a1 = r - 1 + (c - 1)
	max = a1
	a2 = abs(r - 1) + abs(c - m)
	if a2 > max:
		max = a2
	a3 = abs(r - n) + abs(c - 1)
	if a3 > max:
		max = a3
	a4 = abs(r - n) + abs(c - m)
	if a4 > max:
		max = a4
	s.append(max)
for j in s:
	print(j)


for i in [*open(0)][1:]:
	(n, m, r, c) = [*map(int, i.split())]
	print(max(abs(1 - r), abs(n - r)) + max(abs(1 - c), abs(m - c)))


m = int(input())
l = []
for i in range(m):
	(x, y, r1, r2) = map(int, input().split())
	a = [1, 1]
	b = [1, y]
	c = [x, 1]
	d = [x, y]
	e = [r1, r2]
	l.append(max(abs(a[0] - e[0]) + abs(a[1] - e[1]), abs(b[0] - e[0]) + abs(b[1] - e[1]), abs(c[0] - e[0]) + abs(c[1] - e[1]), abs(d[0] - e[0]) + abs(d[1] - e[1])))
for i in l:
	print(i)


import math
import sys
import itertools
import fractions
wtf = sys.stdin.read()
wtf = wtf.strip().split('\n')
for t in wtf[1:]:
	(n, m, r, c) = map(int, t.split())
	ans = []
	ans.append(abs(1 - r) + abs(1 - c))
	ans.append(abs(n - r) + abs(1 - c))
	ans.append(abs(1 - r) + abs(m - c))
	ans.append(abs(n - r) + abs(m - c))
	print(max(ans))


t = int(input())
res = []
for i in range(t):
	(m, n, r, c) = list(map(int, input().split()))
	tl = abs(r - 1) + abs(c - 1)
	tr = abs(r - m) + abs(c - 1)
	bl = abs(r - 1) + abs(c - n)
	br = abs(r - m) + abs(c - n)
	res.append(max(tl, tr, bl, br))
for i in range(t):
	print(res[i])


str = input()
n = int(str)
res = list()
for i in range(0, n):
	inp = input()
	arr = inp.split(' ')
	n = int(arr[0])
	m = int(arr[1])
	r = int(arr[2])
	c = int(arr[3])
	lt = r - 1 + c - 1
	rt = r - 1 + m - c
	lb = n - r + c - 1
	rb = n - r + m - c
	res.append(max(lt, rt, lb, rb))
for test in res:
	print(test)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	print(max(c - 1, a - c) + max(d - 1, b - d))


import sys
from math import comb, perm, ceil, factorial, gcd
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
MOD = 10 ** 9 + 7
INF = float('inf')
rl = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(int(input())):
	(n, m, r, c) = rl()
	print(max(n - r, r - 1) + max(m - c, c - 1))


x = int(input())
t = [None] * x
for i in range(x):
	numbers = list(map(int, input().split()))
	t[i] = max(numbers[0] - numbers[2] + numbers[1] - numbers[3], numbers[0] - numbers[2] + numbers[3] - 1, numbers[2] - 1 + numbers[1] - numbers[3], numbers[2] - 1 + numbers[3] - 1)
for i in range(x):
	print(t[i])


for t in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	print(max(n - r, r - 1) + max(m - c, c - 1))


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split(' '))
	x_coordinate = max(r - 1, n - r)
	y_coordinate = max(c - 1, m - c)
	print(x_coordinate + y_coordinate)


t = int(input())
for z in range(t):
	(n, m, r, c) = map(int, input().split())
	a = r - 1
	b = c - 1
	if a <= n - r:
		a = n - r
	if b <= m - c:
		b = m - c
	print(a + b)


t = int(input())
for _ in range(t):
	(n, m, r, c) = (int(i) for i in input().split())
	res = max(r - 1 + c - 1, n - r + m - c, r - 1 + m - c, n - r + c - 1)
	print(res)


t = int(input())
ans = []
for i in range(t):
	s = input().split()
	n = int(s[0])
	m = int(s[1])
	r = int(s[2])
	c = int(s[3])
	if r > n // 2:
		if c > m // 2:
			ans.append(str(r + c - 2))
		else:
			ans.append(str(r - 1 + m - c))
	elif c > m // 2:
		ans.append(str(n - r + c - 1))
	else:
		ans.append(str(m + n - r - c))
print('\n'.join(ans))


t = int(input())
otv = ''
for i in range(0, t):
	(n, m, r, c) = input().split()
	n = int(n)
	r = int(r)
	m = int(m)
	c = int(c)
	if 1 <= r <= n <= 10 ** 9 and 1 <= c <= m <= 10 ** 9:
		x = n - r
		y = m - c
		if x < r:
			x = r - 1
		if y < c:
			y = c - 1
		otv += str(x + y) + '\n'
if 1 <= t <= 10 ** 4:
	print(otv)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	print(max(r + c - 2, n - r + (m - c), r - 1 + m - c, c - 1 + n - r))


from sys import stdin, stdout
input = stdin.readline
print2 = stdout.write

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

def get_list():
	return list(map(int, stdin.readline().split()))
for _ in range(int(input())):
	(n, m, r, c) = get_ints()
	if r > n // 2:
		max_y = r - 1
	else:
		max_y = n - r
	if c > m // 2:
		max_x = c - 1
	else:
		max_x = m - c
	print(max_x + max_y)


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	t = int(input())
	for _ in range(t):
		(n, m, h, w) = map(int, input().split())
		d1 = h - 1 + w - 1
		d2 = abs(h - n) + w - 1
		d3 = h - 1 + abs(w - m)
		d4 = abs(h - n) + abs(w - m)
		res = max(d1, d2, d3, d4)
		print(res)
resolve()


import sys
t = int(input())
for num in range(t):
	(n, m, r, c) = map(int, sys.stdin.readline().split())
	c1 = r - 1 + c - 1
	c2 = n - r + m - c
	c3 = r - 1 + m - c
	c4 = n - r + c - 1
	print(max(c1, c2, c3, c4))


f = []
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	f.append(max(a - c, c - 1) + max(b - d, d - 1))
for i in range(len(f)):
	print(f[i])


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	(n, m, r, c) = map(int, stdin.readline().split())
	dis = max(abs(r - n) + abs(c - m), abs(1 - r) + abs(1 - c), abs(r - n) + abs(c - 1), abs(1 - r) + abs(m - c))
	print(dis)


outputs = []
for i in range(int(input())):
	inbox = input().split()
	outputs.append(max(int(inbox[0]) - int(inbox[2]), int(inbox[2]) - 1) + max(int(inbox[1]) - int(inbox[3]), int(inbox[3]) - 1))
for i in outputs:
	print(i)


t = int(input())
x = []
for h in range(t):
	(n, m, r, c) = map(int, input().split())
	maxim = 0
	if n - r > r - 1:
		maxim += n - r
	else:
		maxim += r - 1
	if m - c > c - 1:
		maxim += m - c
	else:
		maxim += c - 1
	x.append(maxim)
for i in x:
	print(i)


def f(a):
	n = a[0]
	m = a[1]
	r = a[2]
	c = a[3]
	if r > n // 2:
		if c > m // 2:
			return r + c - 2
		else:
			return r + m - c - 1
	elif c > m // 2:
		return n - r + c - 1
	else:
		return n - r + m - c
t = int(input())
b = [[int(x) for x in input().split()] for i in range(t)]
for i in range(t):
	print(f(b[i]))


k = []
z = []
a = []
t = int(input())
for i in range(t):
	str = input()
	a = str.split(' ')
	n = int(a[0])
	m = int(a[1])
	r = int(a[2])
	c = int(a[3])
	z.append(r - 1 + (c - 1))
	z.append(r - 1 + (m - c))
	z.append(n - r + (c - 1))
	z.append(n - r + (m - c))
	k.append(max(z))
	z = []
for i in range(len(k)):
	print(k[i])


import sys
testcases = int(sys.stdin.readline())
ctr = 0
while ctr < testcases:
	line = sys.stdin.readline()
	stringIn = line.split()
	n = int(stringIn[0])
	m = int(stringIn[1])
	r = int(stringIn[2])
	c = int(stringIn[3])
	tmax = 0
	trowU = r - 1
	trowD = n - r
	tRow = trowU
	if trowD > tRow:
		tRow = trowD
	tcolL = c - 1
	tcolR = m - c
	tCol = tcolL
	if tcolR > tCol:
		tCol = tcolR
	tmax = tRow + tCol
	print(tmax)
	ctr += 1


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	e = a
	f = b
	g = a - c
	h = b - d
	if c > (a + 1) / 2:
		e = 1
		g = c - 1
	if d > (b + 1) / 2:
		f = 1
		h = d - 1
	print(g + h)


import sys
for i in range(int(sys.stdin.readline())):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	ans = max(a - c + b - d, c - 1 + d - 1, a - c + d - 1, b - d + c - 1)
	print(ans)


import math

def find(roomHeight, roomWidth, exitY, exitX):
	if roomWidth - exitX <= roomWidth - (roomWidth - exitX + 1):
		curX = exitX - 1
	else:
		curX = roomWidth - exitX
	if roomHeight - exitY <= roomHeight - (roomHeight - exitY + 1):
		curY = exitY - 1
	else:
		curY = roomHeight - exitY
	return str(curX + curY)
arr = []
for i in range(int(input())):
	(roomHeight, roomWidth, exitY, exitX) = map(int, input().split())
	arr.append(find(roomHeight, roomWidth, exitY, exitX))
print('\n'.join(arr))


cases = [list(map(int, input().split())) for i in range(int(input()))]
for (i, a) in enumerate(cases):
	n = a[0]
	m = a[1]
	i = a[2]
	j = a[3]
	result = max(i - 1, n - i) + max(j - 1, m - j)
	print(result)


c_tests = int(input())
for _ in range(c_tests):
	(n, m, r, c) = [int(__) for __ in input().split()]
	print(max(r - 1 + c - 1, r - 1 + m - c, n - r + c - 1, n - r + m - c))


tests = int(input())
for x in range(tests):
	(n, m, r, c) = input().split()
	(n, m, r, c) = (int(n), int(m), int(r) - 1, int(c) - 1)
	best = 0
	if r + c > best:
		best = r + c
	if n - 1 - r + c > best:
		best = n - 1 - r + c
	if r + (m - 1 - c) > best:
		best = r + (m - 1 - c)
	if n - 1 - r + (m - 1 - c) > best:
		best = n - 1 - r + (m - 1 - c)
	print(best)


t = int(input())
while t:
	(n, m, r, c) = map(int, input().split())
	print(max(n - r, r - 1) + max(m - c, c - 1))
	t -= 1


t = int(input())
for i in range(0, t):
	(n, m, r, c) = [int(x) for x in input().split()]
	min_num = max(r - 1, n - r) + max(c - 1, m - c)
	print(min_num)


import math

def solve():
	intputt = input().split()
	n = int(intputt[0])
	m = int(intputt[1])
	r = int(intputt[2])
	c = int(intputt[3])
	fromtopleft = abs(r - 1) + abs(c - 1)
	fromtopright = abs(r - 1) + abs(m - c)
	frombottomleft = abs(n - r) + abs(c - 1)
	frombottomright = abs(n - r) + abs(m - c)
	print(max(fromtopleft, max(fromtopright, max(frombottomleft, frombottomright))))
t = int(input())
for i in range(0, t):
	solve()


def func():
	n = int(input())
	for _ in range(n):
		(a, b, c, d) = map(int, input().strip().split())
		print(max((abs(c - a) + abs(d - b), abs(c - 1) + abs(d - b), abs(c - a) + abs(d - 1), abs(c - 1) + abs(d - 1))))
func()


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	distance = max(abs(r - 1), abs(r - n)) + max(abs(c - 1), abs(c - m))
	print(distance)


t = int(input())
a = []
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	a.append(max(abs(1 - r), abs(n - r)) + max(abs(1 - c), abs(m - c)))
for i in range(len(a)):
	print(a[i])


t = int(input())
a = []
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	a.append(str(max(r - 1, n - r) + max(c - 1, m - c)))
print('\n'.join(a))


t = int(input())
a = []
for q in range(t):
	(n, m, r, c) = list(map(int, input().split()))
	a.append(max(n - r, n - (n - r) - 1) + max(m - c, m - (m - c) - 1))
for q in range(t):
	print(a[q])


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	ans = max(r + c - 2, n - r + c - 1, r + m - c - 1, n - r + m - c)
	print(ans)


t = int(input())
ans = []
for i in range(t):
	(n, m, r, c) = list(map(int, input().split()))
	(h, w) = (1, 1)
	if r <= (n + 1) // 2:
		h = n
	if c <= (m + 1) // 2:
		w = m
	ans.append(abs(h - r) + abs(w - c))
for i in ans:
	print(i)


def numb_1():
	(n, m, r, c) = map(int, input().split(' '))
	return max(abs(n - r) + abs(m - c), abs(r - 1) + abs(c - 1), abs(n - r) + abs(c - 1), abs(r - 1) + abs(m - c))
t = int(input())
res = []
for i in range(t):
	res.append(numb_1())
for item in res:
	print(item)


t = int(input())
a = []
for i in range(t):
	nmrc = input()
	nmrc = nmrc.split()
	c = 0
	if int(nmrc[0]) / 2 < int(nmrc[2]):
		c += int(nmrc[2]) - 1
	else:
		c += int(nmrc[0]) - int(nmrc[2])
	if int(nmrc[1]) / 2 < int(nmrc[3]):
		c += int(nmrc[3]) - 1
	else:
		c += int(nmrc[1]) - int(nmrc[3])
	a.append(c)
for b in a:
	print(b)


p = int(input())
A = []
for x in range(p):
	(n, m, r, c) = map(int, input().split())
	r1 = r - 1 + c - 1
	r2 = m - c + r - 1
	r3 = n - r + m - c
	r4 = n - r + c - 1
	A.append(max(r1, r2, r3, r4))
for x in range(len(A)):
	print(A[x])


def main(n, m, r, c):
	return max(r - 1, n - r) + max(c - 1, m - c)
rc = []
for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	rc.append(main(n, m, r, c))
print(*rc, sep='\n')


t = int(input())
o = []
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	a = r + c - 2
	a1 = n - r + m - c
	a2 = r + m - c - 1
	a3 = n - r + c - 1
	o.append(max(a, a1, a2, a3))
for i in range(t):
	print(o[i])


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
result = []

def get_result(n, m, r, c):
	return max(r - 1, n - r) + max(c - 1, m - c)
for i in range(t):
	case = inlt()
	print(get_result(*case))


result = []
for i in range(int(input())):
	(n, m, r, c) = (int(i) for i in input().split())
	if n / 2 >= r and m / 2 >= c:
		result.append(n - r + m - c)
	elif n / 2 >= r and m / 2 <= c:
		result.append(n - r + c - 1)
	elif n / 2 <= r and m / 2 >= c:
		result.append(r + m - c - 1)
	elif n / 2 <= r and m / 2 <= c:
		result.append(r + c - 2)
print(*result, sep='\n')


t = input()
input_data = []
for i in range(int(t)):
	(n, m, r, c) = input().split()
	input_data.append((int(n), int(m), int(r), int(c)))
for (n, m, r, c) in input_data:
	dif1 = abs(1 - r) + abs(1 - c)
	dif2 = abs(n - r) + abs(m - c)
	dif3 = abs(1 - r) + abs(m - c)
	dif4 = abs(n - r) + abs(1 - c)
	print(max(dif1, dif2, dif3, dif4))


a = []
t = int(input())
for i in range(t):
	a.append(input())
for i in a:
	ma1 = 0
	ma2 = 0
	(n, m, r, c) = map(int, i.split())
	ma1 = max(abs(1 - r), abs(n - r))
	ma2 = max(abs(1 - c), abs(m - c))
	print(ma1 + ma2)


t = int(input())
a = []
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	ld = r - 1 + c - 1
	lu = m - c + r - 1
	rd = n - r + c - 1
	ru = m - c + n - r
	a.append(max(ld, lu, rd, ru))
for k in range(len(a)):
	print(a[k])


t = int(input())
result = []
for _ in range(t):
	inp = input().split()
	[n, m, r, c] = [int(i) for i in inp]
	res = 0
	if r <= n // 2:
		res += n - r
	else:
		res += r - 1
	if c <= m // 2:
		res += m - c
	else:
		res += c - 1
	result.append(res)
print(*result, sep='\n')


case = int(input())
lines = []
for i in range(0, case):
	lines.append(input())
for i in lines:
	i = i.split()
	n = int(i[0])
	m = int(i[1])
	r = int(i[2])
	c = int(i[3])
	print(max(n - r + (m - c), r - 1 + (c - 1), r - 1 + (m - c), n - r + (c - 1)))


g = []
for _ in range(int(input())):
	(m, n, r, c) = map(int, input().split())
	if m - r >= abs(1 - r):
		max1 = m - r
	else:
		max1 = abs(1 - r)
	if n - c >= abs(1 - c):
		max2 = n - c
	else:
		max2 = abs(1 - c)
	g.append(max1 + max2)
for i in g:
	print(i)


t = int(input())
for _ in range(t):
	(y, x, r, c) = map(int, input().split())
	x1 = abs(x - c)
	x2 = abs(1 - c)
	y1 = abs(y - r)
	y2 = abs(1 - r)
	gx = x1 if x1 >= x2 else x2
	gy = y1 if y1 >= y2 else y2
	print(gx + gy)


for i in range(int(input())):
	(r, c, i, j) = map(int, input().split())
	a = abs(1 - i) + abs(1 - j)
	b = abs(r - i) + abs(1 - j)
	d = abs(1 - i) + abs(c - j)
	e = abs(r - i) + abs(c - j)
	print(max(a, b, d, e))


def minimal_time(n, m, r, c):
	v_steps = max(n - r, r - 1)
	h_steps = max(m - c, c - 1)
	return v_steps + h_steps
t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	print(minimal_time(n, m, r, c))


a = int(input())
for i in range(a):
	(n, m, r, c) = [int(b) for b in input().split()]
	print(max(n - r + m - c, r - 1 + c - 1, n - r + c - 1, r - 1 + m - c))


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	n -= 1
	m -= 1
	r -= 1
	c -= 1
	xcen = m / 2
	ycen = n / 2
	if r >= ycen and c >= xcen:
		print(abs(r - 0) + abs(c - 0))
	elif r <= ycen and c >= xcen:
		print(abs(r - n) + abs(c - 0))
	elif r >= ycen and c <= xcen:
		print(abs(r - 0) + abs(c - m))
	elif r <= ycen and c <= xcen:
		print(abs(r - n) + abs(c - m))


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	d1 = abs(1 - r) + abs(1 - c)
	d2 = abs(1 - r) + abs(m - c)
	d3 = abs(n - r) + abs(1 - c)
	d4 = abs(n - r) + abs(m - c)
	m = max(d1, d2, d3, d4)
	print(m)


cases = int(input())
while cases:
	cases -= 1
	(n, m, r, c) = map(int, input().split())
	ans = max(abs(n - r) + abs(m - c), abs(1 - r) + abs(1 - c), abs(1 - r) + abs(m - c), abs(n - r) + abs(1 - c))
	print(ans)


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	rowc = max(r - 1, n - r)
	colc = max(c - 1, m - c)
	print(rowc + colc)


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	q = max(r - 1, n - r)
	w = max(c - 1, m - c)
	print(q + w)


for x in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	row_dis = max(n - r, r - 1)
	col_dis = max(m - c, c - 1)
	print(row_dis + col_dis)