Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Leha is playing a very interesting game. The game will be played on a rectangular grid consisting of N rows and M columns. Initially all the cells of the grid are uncolored. 

Leha's initial score is zero. At each turn, he chooses some cell that is yet not colored, and colors that cell. The score obtained in this step will be number of neighboring colored cells of the cell that Leha colored in this step. Two cells are neighbors of each other if they share a side between them. The game will end when all the cells are colored. Finally, total score obtained at the end of the game will sum of score obtained in each turn.

Leha wants to know what maximum score he can get? Can you please help him in finding this out?

------ Input ------ 

The first line contains a single integer T denoting the number of test cases. T test cases follow.
Each of the following T lines contains two space-separated integers N, M denoting the dimensions of the grid.

------ Output ------ 

For each test case, output a single line containing an integer corresponding to the maximal possible score Leha can obtain.

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

$1 ≤ T ≤ 100$
$1 ≤ N, M ≤ 1 000$

------ Subtasks ------ 

$Subtask #1[30 points]: 1 ≤ N, M ≤ 3$
$Subtask #2[70 points]: 1 ≤ N, M ≤ 1 000$

----- Sample Input 1 ------ 
1
2 2
----- Sample Output 1 ------ 
4
----- explanation 1 ------ 
Leha can obtain total score 4 in the following way.

In the first step, he colors down left cell, all the neighboring cells of this cell are uncolored. So, it adds 0 to total score.
In second step, he can color upper right cell, it also adds total 0 to the score.
In third step, he can color top left cell. There are two neighboring cell of this cell, both of which are colored. So, this add 2 to the score.
In the last step, he can choose the remaining down right cell. There are two neighboring cell of this cell, both of which are colored. So, this also add 2 to the score.

Leha can't obtain a score more than 4. Hence 4 is the answer.

Difficulty: MEDIUM
Tags: Graph algorithms

Data structures
Skill types: Data structures

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	res = 2 * m * n - m - n
	print(res)


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


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	ans = 2 * a * b - a - b
	print(ans)


T = int(input())
for i in range(T):
	(N, M) = map(int, input().split())
	maxscore = N * M
	print(2 * maxscore - N - M)


for p in range(int(input())):
	(n, m) = list(map(int, input().split()))
	graph = [[0] * m for _ in range(n)]
	score = 0
	print(n - 1 + (m - 1) + (m - 1) * (n - 1) * 2)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	if x == 1 and y == 1:
		print(0)
	else:
		print(2 * x * y - x - y)


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


for _ in range(int(input())):
	(n, m) = [int(a) for a in input().split()]
	print(2 * m * n - m - n)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	else:
		print(2 * m * n - n - m)


for tc in range(int(input())):
	(n, m) = map(int, input().split())
	tot = m - 1
	tot += n - 1
	tot += (n - 1) * (m - 1) * 2
	print(tot)


t = int(input())
while t:
	(n, m) = map(int, input().split())
	x = m - 1 + (n - 1) * (1 + 2 * (m - 1))
	print(x)
	t -= 1


T = int(input())
for tj in range(T):
	(N, M) = map(int, input().split())
	if N == 1 or M == 1:
		print(max(N, M) - 1)
	else:
		print(M + 2 * N * M - N - 2 * M)


def function(N, M):
	val1 = M + N - 2
	val2 = (M - 1) * (N - 1) * 2
	return val1 + val2

def main():
	T = int(input())
	for _ in range(T):
		(N, M) = list(map(int, input().split(' ')))
		val = function(N, M)
		print(val)
main()


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	ans = 2 * n * m - n - m
	print(ans)


t = int(input())
while t != 0:
	(n, m) = map(int, input().split())
	print(2 * m * n - m - n)
	t -= 1


t = int(input())
for tc in range(t):
	(n, m) = map(int, input().split())
	if n == 0 and m == 0:
		print('0')
	ans = 2 * (m - 1) * (n - 1) + m - 1 + n - 1
	print(ans)


t = int(input())
l = list()
for _ in range(t):
	(r, c) = map(int, input().split())
	sum = 0
	sum += (r - 1) * 1
	sum += (c - 1) * 1
	sum += (r * c - (r + (c - 1))) * 2
	l.append(sum)
for i in l:
	print(i)


tc = int(input())
while tc:
	(r, c) = tuple(map(int, input().split()))
	ones = c - 1 + (r - 1)
	twos = (c - 1) * (r - 1)
	val = ones * 1 + twos * 2
	print(val)
	tc -= 1


for _ in range(int(input())):
	b = list(map(int, input().split()))
	(m, n) = (b[0], b[1])
	print(int((2 * n - 1) * (m - 1) + (n - 1)))


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	print(2 * (M - 1) * (N - 1) + M + N - 2)


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


Input = [[int(i) for i in input().split(' ')] for i in range(0, int(input()))]
for i in Input:
	print(2 * i[0] * i[1] - i[0] - i[1])


for _ in range(int(input())):
	(r, c) = [int(x) for x in input().split(' ')]
	print(2 * r * c - r - c)


t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	if n == 1 and m == 1:
		print(0)
		continue
	ans = 2 * m * n - m - n
	print(ans)


t = int(input())
for _ in range(t):
	(r, c) = map(int, input().split())
	tot = r * (c - 1) + c * (r - 1)
	print(tot)


def ge(x):
	return 1 + 2 * (x - 1)
l = [0, 0, 4]
for i in range(2, 1001):
	r = ge(i)
	e = ge(i + 1)
	l.append(l[i] + r + e)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == m:
		print(l[n])
	elif n > m:
		print(l[m] + ge(m) * (n - m))
	else:
		print(l[n] + ge(n) * (m - n))


def max(n, m):
	if m == 1 and n == 1:
		return 0
	else:
		return 2 * m * n - m - n
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	res = max(n, m)
	print(res)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = 0
	for i in range(n):
		if i == 0:
			s = s + (m - 1)
		else:
			s = s + (1 + 2 * (m - 1))
	print(s)


t = int(input())
for i in range(t):
	(l, w) = map(int, input().split())
	total = l * (w * 2 - 1) - w
	print(total)


t = int(input())
for i in range(t):
	num = 2
	(a, b) = map(int, input().split(' '))
	print(num * a * b - a - b)


tests = int(input())
for i in range(tests):
	(n, m) = map(int, input().split())
	ones = n + m - 2
	remaining = (n - 1) * (m - 1) * 2
	print(ones + remaining)


output = ''
for i in range(int(input())):
	arr = list(map(int, input().split()))
	output += str((arr[0] - 1) * arr[1] + (arr[1] - 1) * arr[0]) + '\n'
print(output)


t = int(input())
for i in range(t):
	(o, p) = map(int, input().split())
	totalmanoj = o * (p * 2 - 1) - p
	print(totalmanoj)


n = int(input())
for i in range(n):
	(r, c) = map(int, input().split())
	if r == 0 and c == 0:
		print('0')
	ans = 2 * (c - 1) * (r - 1) + c - 1 + r - 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	print(n * (m - 1) + m * (n - 1))