Chef has started developing interest in playing chess, and was learning how the [Queen] moves. 

Chef has an empty N \times N chessboard. He places a Queen at (X, Y) and wonders - What are the number of cells that are under attack by the Queen? 

Notes:
The top-left cell is (1, 1), the top-right cell is (1, N), the bottom-left cell is (N,1) and the bottom-right cell is (N, N).
The Queen can be moved any number of unoccupied cells in a straight line vertically, horizontally, or diagonally.
The cell on which the Queen is present, is not said to be under attack by the Queen.

------ Input Format ------ 

- The first line contains a single integer T - the number of test cases. Then the test cases follow.
- The first and only line of each test case contains three integers N, X and Y, as described in the problem statement. 

------ Output Format ------ 

For each test case, output in a single line, the total number of cells that are under attack by the Queen. 

------ Constraints ------ 

$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ X, Y ≤ N$

----- Sample Input 1 ------ 
5
1 1 1
3 2 2
3 2 1
2 2 2
150 62 41

----- Sample Output 1 ------ 
0
8
6
3
527

----- explanation 1 ------ 
Test case 1: The only cell on the board is $(1,1)$. Since Queen stands on this cell, it is not under attack.

Test case 2: The Queen can attack the following cells: $\{(1, 1), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (3, 3)\}$.

Test case 3: The Queen can attack the following cells: $\{(1, 1), (1, 2), (2, 2), (2, 3), (3, 1), (3, 2)\}$.

Test case 4: The Queen can attack the following cells: $\{(1, 1), (1, 2), (2, 1)\}$.

Source: codechef | Difficulty: EASY
Tags: Mathematics


Solutions:
for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	st = 2 * n - 2
	cross = 0
	if x == 1 or y == 1 or x == n or (y == n):
		cross = n - 1
	else:
		cross = min(x, y) - 1 + (n - max(x, y))
		cross += min(x - 1, n - y) + min(n - x, y - 1)
	print(st + cross)


for i in range(int(input())):
	(n, x, y) = map(int, input().split())
	print(2 * (n - 1) + min(x - 1, y - 1) + min(x - 1, n - y) + min(n - x, y - 1) + min(n - x, n - y))


for t in range(int(input())):
	(num, r, m) = map(int, input().split())
	print(2 * (num - 1) + min(r - 1, m - 1) + min(r - 1, num - m) + min(num - r, m - 1) + min(num - r, num - m))


for _ in range(int(input())):
	(x, y, z) = map(int, input().split())
	if y == 1 or z == 1:
		print((x - 1) * 2 + (x - 1))
	else:
		c = x - 1 + (x - 1)
		ur = min(y - 1, x - z)
		ul = min(y - 1, z - 1)
		bl = min(x - y, z - 1)
		br = min(x - y, x - z)
		print(c + ur + ul + bl + br)


for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	if n == 1:
		print('0')
		continue
	if n == 2:
		print('3')
		continue
	rud = min(n - x, n - y)
	rld = min(n - x, y - 1)
	lud = min(x - 1, n - y)
	lld = min(x - 1, y - 1)
	print(rud + rld + lud + lld + (n - 1) * 2)


def queen():
	for i in range(int(input())):
		(num, r, m) = map(int, input().split())
		print(2 * (num - 1) + min(r - 1, m - 1) + min(r - 1, num - m) + min(num - r, m - 1) + min(num - r, num - m))
queen()


for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	vert_hor = 2 * (n - 1)
	left_diag = min(x - 1, y - 1) + min(x - 1, n - y)
	right_diag = min(n - x, y - 1) + min(n - x, n - y)
	sum = vert_hor + left_diag + right_diag
	print(sum)


for t in range(int(input())):
	(n, x, y) = map(int, input().split())
	a = min(x - 1, n - y)
	b = min(x - 1, y - 1)
	c = min(n - x, y - 1)
	d = min(n - x, n - y)
	print(2 * (n - 1) + a + b + c + d)


for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	ans = 2 * (n - 1)
	x -= 1
	y -= 1
	for _ in range(4):
		ans += min(x, y)
		(x, y) = (y, n - 1 - x)
	print(ans)


t = int(input())
for j in range(t):
	(n, x, y) = map(int, input().split())
	print(2 * (n - 1) + min(y - 1, x - 1) + min(y - 1, n - x) + min(n - x, n - y) + min(n - y, x - 1))


for i in range(int(input())):
	(n, x, y) = map(int, input().split())
	s = 2 * (n - 1)
	d1 = min(x - 1, y - 1)
	d2 = min(n - x, n - y)
	d3 = min(n - x, y - 1)
	d4 = min(n - y, x - 1)
	s = s + d1 + d2 + d3 + d4
	print(s)


for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	k = 2 * (n - 1) + min(x - 1, y - 1) + min(x - 1, n - y) + min(n - x, y - 1) + min(n - x, n - y)
	print(k)


T = int(input())
for i in range(T):
	(N, X, Y) = map(int, input().split())
	d = min(X - 1, Y - 1) + min(N - X, N - Y) + min(N - X, Y - 1) + min(X - 1, N - Y)
	print(2 * N - 2 + d)


for T in range(int(input())):
	(n, x, y) = map(int, input().split())
	h = n - 1
	v = n - 1
	d1 = n - abs(x + y - n - 1) - 1
	d2 = n - abs(y - x) - 1
	print(h + v + d1 + d2)


for _ in range(int(input())):
	(N, X, Y) = map(int, input().split())
	HV = 2 * N - 2
	D1 = min(X - 1, Y - 1) + min(N - X, N - Y)
	D2 = min(X - 1, N - Y) + min(N - X, Y - 1)
	print(HV + D1 + D2)


for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	s = y - 1 + (n - y) + (n - x) + (x - 1) + min(y - 1, x - 1) + min(n - y, x - 1) + min(n - y, n - x) + min(y - 1, n - x)
	print(s)


t = int(input())
for _ in range(t):
	(x, y, z) = map(int, input().split())
	print(2 * (x - 1) + min(y - 1, z - 1) + min(y - 1, x - z) + min(x - y, z - 1) + min(x - y, x - z))


t = int(input())
for i in range(t):
	(n, x, y) = map(int, input().split())
	a = n - 1
	b = n - 1
	c = min(x - 1, n - y)
	d = min(n - y, n - x)
	e = min(x - 1, y - 1)
	f = min(n - x, y - 1)
	print(a + b + c + d + e + f)


for i in range(int(input())):
	(n, x, y) = map(int, input().split())
	if x == 1 and y == 1 or (x == n and y == n):
		print(3 * (n - 1))
	else:
		print(2 * (n - 1) + min(x - 1, y - 1) + min(x - 1, n - y) + min(n - x, y - 1) + min(n - x, n - y))


t = int(input())
for i in range(t):
	[n, a, b] = list(map(int, input().split()))
	adj_moves = 2 * (n - 1)
	dm = 0
	h = {'d': n - a, 'u': a - 1}
	v = {'r': n - b, 'l': b - 1}
	for j in h.keys():
		for k in v.keys():
			dm += min(h[j], v[k])
	print(adj_moves + dm)


for _ in range(int(input())):
	(n, x, y) = map(int, input().split(' '))
	v_h = 2 * (n - 1)
	d = min(x, y) - 1 + n - max(x, y)
	if x + y <= n:
		d += max(x, y) + min(x, y) - 2
	else:
		d += 2 * n - (min(x, y) + max(x, y))
	print(v_h + d)


t = int(input())
for i in range(t):
	cells = input().split(' ')
	ver_hor = 2 * (int(cells[0]) - 1)
	dig_up = min(int(cells[0]) - int(cells[1]), int(cells[0]) - int(cells[2]))
	dig_down = min(int(cells[1]) - 1, int(cells[2]) - 1)
	dig_up_l = min(abs(1 - int(cells[1])), abs(int(cells[0]) - int(cells[2])))
	dig_up_r = min(abs(1 - int(cells[2])), abs(int(cells[0]) - int(cells[1])))
	print(ver_hor + dig_down + dig_up + dig_up_l + dig_up_r)


from itertools import product
for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	count = 2 * (n - 1) + min(x - 1, y - 1) + min(x - 1, n - y) + min(n - x, y - 1) + min(n - x, n - y)
	print(count)


t = int(input())
for tc in range(t):
	(n, x, y) = map(int, input().split())
	a = n * n
	s = x - 1 + (n - x) + (y - 1) + (n - y) + min(x - 1, y - 1) + min(y - 1, n - x) + min(n - x, n - y) + min(n - y, x - 1)
	print(s)


t = int(input())
for i in range(t):
	(n, x, y) = map(int, input().split())
	s = x - 1 + (n - x) + (y - 1) + (n - y) + min(x - 1, y - 1) + min(y - 1, n - x) + min(n - x, n - y) + min(n - y, x - 1)
	print(s)


t = int(input())
for _ in range(t):
	(n, x, y) = map(int, input().split())
	if n == 1:
		print(0)
	else:
		k = n - x + (n - y) + (x - 1) + (y - 1) + min(x - 1, y - 1) + min(n - x, n - y) + min(x - 1, n - y) + min(n - x, y - 1)
		print(k)


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


from math import dist
for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	if n % 2:
		center = (-(-n // 2), -(-n // 2))
		print(4 * n - 4 - 2 * max(abs(x - int(center[0])), abs(y - int(center[1]))))
	else:
		center_even = [[x, y] for x in range(-(-n // 2), -(-n // 2) + 2) for y in range(-(-n // 2), -(-n // 2) + 2)]
		distance = {tuple(c): dist((x, y), c) for c in center_even}
		center = min(distance, key=distance.get)
		print(4 * n - 5 - 2 * max(abs(x - int(center[0])), abs(y - int(center[1]))))


x = int(input())
for i in range(x):
	(s, x, y) = input().split(' ')
	(s, x, y) = map(int, (s, x, y))
	at = 2 * (s - 1)
	at += min(x - 1, y - 1)
	at += min(x - 1, s - y)
	at += min(s - x, y - 1)
	at += min(s - x, s - y)
	print(at)


t = int(input())
for i in range(t):
	(N, X, Y) = map(int, input().split())
	x = N - 1
	y = N - 1
	z = 2 * N - 2 - abs(X - Y) - abs(X + Y - N - 1)
	print(x + y + z)


for _ in range(int(input())):
	(n, x, y) = map(int, input().split())
	verticalHorizontal = n - 1 + n - 1
	diagonal = 0
	diagonal += min(x - 1, y - 1)
	diagonal += min(x - 1, n - y)
	diagonal += min(n - x, y - 1)
	diagonal += min(n - x, n - y)
	diagonal += verticalHorizontal
	print(diagonal)


T = int(input())
while T > 0:
	(N, X, Y) = map(int, input().split())
	x = N - 1
	y = N - 1
	z = 2 * N - 2 - abs(X - Y) - abs(X + Y - N - 1)
	print(x + y + z)
	T -= 1


T = int(input())
for _ in range(T):
	(N, X, Y) = map(int, input().split())
	su = 2 * N - 2
	if X + Y <= N + 1:
		su += X + Y - 1 - 1
	else:
		(X, Y) = (N + 1 - X, N + 1 - Y)
		su += X + Y - 1 - 1
		(X, Y) = (N + 1 - X, N + 1 - Y)
	t = abs(X - Y)
	su += N - 1 - t
	print(su)


T = int(input())
for t in range(T):
	l = list(map(int, input().split()))
	n = l[0]
	x = l[1]
	y = l[2]
	print(2 * (n - 1) + min(x - 1, y - 1) + min(x - 1, n - y) + min(n - x, y - 1) + min(n - x, n - y))


t = int(input())
for _ in range(t):
	(n, x, y) = map(int, input().split(' '))
	m1 = n + 1 - x
	m2 = n + 1 - y
	move1 = min(x, y)
	move2 = min(m1, m2)
	M = min(move1, move2)
	max_move = 3 * (n - 1) + 2 * (M - 1)
	print(max_move)


def queen(n, x, y):
	return n - 1 + (n - 1) + min(x - 1, y - 1) + min(n - x, n - y) + min(n - x, y - 1) + min(x - 1, n - y)
for _ in range(int(input())):
	(a, b, c) = map(int, input().strip().split())
	print(queen(a, b, c))