You are given a grid with $n$ rows and $m$ columns. Rows and columns are numbered from $1$ to $n$, and from $1$ to $m$. The intersection of the $a$-th row and $b$-th column is denoted by $(a, b)$.

Initially, you are standing in the top left corner $(1, 1)$. Your goal is to reach the bottom right corner $(n, m)$.

You can move in four directions from $(a, b)$: up to $(a-1, b)$, down to $(a+1, b)$, left to $(a, b-1)$ or right to $(a, b+1)$.

You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach $(n, m)$?


-----Input-----

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^3$) — the number of the test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^9$) — the size of the grid.


-----Output-----

For each test case, print a single integer: $-1$ if it is impossible to reach $(n, m)$ under the given conditions, otherwise the minimum number of moves.


-----Examples-----

Input
6
1 1
2 1
1 3
4 2
4 6
10 5
Output
0
1
-1
6
10
17


-----Note-----

Test case $1$: $n=1$, $m=1$, and initially you are standing in $(1, 1)$ so $0$ move is required to reach $(n, m) = (1, 1)$.

Test case $2$: you should go down to reach $(2, 1)$.

Test case $3$: it is impossible to reach $(1, 3)$ without moving right two consecutive times, or without leaving the grid.

Test case $4$: an optimal moving sequence could be: $(1, 1) \to (1, 2) \to (2, 2) \to (2, 1) \to (3, 1) \to (3, 2) \to (4, 2)$. It can be proved that this is the optimal solution. So the answer is $6$.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation


Solutions:
import sys
input = sys.stdin.readline

def solve():
	(n, m) = sorted(map(int, input().split()))
	if n == 1 and m > 2:
		print(-1)
		return
	print((m - 1) * 2 - (m - n) % 2)
for _ in [0] * int(input()):
	solve()


for i in [*open(0)][1:]:
	(n, m) = sorted(map(int, i.split()))
	print([(n - 1) * 2 + (m - n) // 2 * 4 + (m - n) % 2, [(m == 1) - 1, 1][m == 2]][n == 1])


test = eval(input(''))
rows = []
column = []
for i in range(test):
	(x, y) = [int(x) for x in input().split()]
	rows.append(x)
	column.append(y)
for j in range(len(rows)):
	count = 0
	if rows[j] == 1 and column[j] == 1:
		print(0)
	elif rows[j] == 1 and column[j] > 2 or (rows[j] > 2 and column[j] == 1):
		print(-1)
	elif rows[j] == column[j]:
		print(rows[j] + column[j] - 2)
	else:
		if rows[j] > column[j]:
			count += column[j] * 2 - 2
		else:
			count += rows[j] * 2 - 2
		diff = abs(column[j] - rows[j]) + 1
		if diff % 2 == 1:
			count += 2 * diff - 2
		else:
			count += 2 * diff - 3
		print(count)


import sys
N = int(sys.stdin.readline().strip())
for _ in range(N):
	(n, m) = map(int, sys.stdin.readline().strip().split(' '))
	if abs(n - m) >= 2:
		if n == 1 or m == 1:
			print(-1)
		else:
			print(m + n - 2 + abs(n - m) // 2 * 2)
	else:
		print(n + m - 2)


import math
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n == 1 and m == 2:
		print(1)
	elif n == 2 and m == 1:
		print(1)
	elif n == 1 or m == 1:
		print(-1)
	elif n == m:
		print(2 * (n - 1))
	else:
		ans = (min(n, m) - 1) * 2
		diff = max(m, n) - min(m, n)
		if diff % 2 == 0:
			ans = ans + diff * 2
		else:
			ans = ans + (diff - 1) * 2
			ans = ans + 1
		print(ans)


def sol(x):
	if x % 2 == 0:
		return 2 * x
	else:
		return 2 * (x - 1) + 1
t = int(input())
for _ in range(t):
	m = input().split()
	a = max(int(m[0]), int(m[1]))
	b = min(int(m[0]), int(m[1]))
	if b == 1:
		if a == 1:
			print(0)
		elif a == 2:
			print(1)
		else:
			print(-1)
	else:
		print(2 * (b - 1) + sol(a - b))


t = int(input())
otv = [0] * t
for T in range(t):
	(n, m) = map(int, input().split())
	(n, m) = (max(n, m), min(m, n))
	if m == 1 and n - m > 1:
		otv[T] = -1
		continue
	if n - m >= 2:
		otv[T] = m + n + (n - m) // 2 * 2 - 2
	else:
		otv[T] = m + n - 2
for i in range(t):
	print(otv[i])


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 and m > 2:
		print(-1)
	elif m == 1 and n > 2:
		print(-1)
	elif n == m:
		print(2 * (n - 1))
	else:
		mn = min(m, n)
		mx = max(m, n)
		ans = 2 * (mn - 1)
		if (mx - mn) % 2 == 1:
			ans += 2 * (mx - mn) - 1
		else:
			ans += 2 * (mx - mn)
		print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n < m:
		(n, m) = (m, n)
	if n > 2 and m == 1:
		print(-1)
	elif n - m < 2:
		print(n + m - 2)
	else:
		x = n - m - 1
		if x % 2 == 1:
			x += 1
		print(n + m - 2 + x)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m > 2:
		print(-1)
	elif n > 2 and m == 1:
		print(-1)
	elif abs(n - m) < 2:
		print(n + m - 2)
	else:
		print(min(n, m) * 2 - 2 + 2 * (max(n, m) - min(n, m)) - (n - m) % 2)


noot = int(input())
for nool in range(noot):
	(a, b) = [int(x) for x in input().split()]
	(a, b) = (min(a, b), max(a, b))
	if a == 1:
		if b - a > 1:
			print(-1)
		else:
			print(b + a - 2)
	else:
		ans = 2 * a - 2
		if (b - a) % 2 == 0:
			print(ans + (b - a) * 2)
		else:
			print(ans + (b - 1 - a) * 2 + 1)


t = int(input())

def changer(a):
	if a == '1':
		return '0'
	return '1'

def solve():
	(n, m) = map(int, input().split())
	if min(n, m) == 1 and max(n, m) > 2:
		print(-1)
		return
	if min(n, m) == 1 and max(n, m) == 1:
		print(0)
		return
	if min(n, m) == 1 and max(n, m) == 2:
		print(1)
		return
	sq = 2 * min(n, m) - 2
	if (max(n, m) - min(n, m)) % 2 == 1:
		print(sq + 2 * (max(n, m) - min(n, m)) - 1)
		return
	print(sq + 2 * (max(n, m) - min(n, m)))
for i in range(t):
	solve()


import sys
inp = sys.stdin.readline()
test = int(inp.strip('\n'))
for i in range(test):
	inp = sys.stdin.readline()
	(n, m) = list(map(int, inp.strip('\n').split()))
	n -= 1
	m -= 1
	if n == 0 and m == 0:
		sys.stdout.write('0\n')
	elif n == 0:
		if m > 1:
			sys.stdout.write('-1\n')
		else:
			sys.stdout.write('1\n')
	elif m == 0:
		if n > 1:
			sys.stdout.write('-1\n')
		else:
			sys.stdout.write('1\n')
	else:
		val = n + m + abs(n - m)
		if abs(n - m) % 2:
			val -= 1
		sys.stdout.write(str(val) + '\n')


def solve():
	x = 2 * abs(min(n, m) - 1)
	if n == 1 and m == 1:
		return 0
	if n == 1 and m > 2 or (m == 1 and n > 2):
		return -1
	if n >= m:
		if (n - m) % 2 == 0:
			x += (n - m) // 2 + 3 * (n - m) // 2
		else:
			x += 1 + (n - m - 1) // 2 + 3 * (n - m - 1) // 2
	elif (m - n) % 2 == 0:
		x += (m - n) // 2 + 3 * (m - n) // 2
	else:
		x += 1 + (m - n - 1) // 2 + 3 * (m - n - 1) // 2
	return x
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	print(solve())


a = int(input())
for aa in range(a):
	b = input().split(' ')
	b[0] = int(b[0]) - 1
	b[1] = int(b[1]) - 1
	if b[0] - b[1] == 0:
		print(2 * b[0])
	elif b[0] - b[1] == 1:
		print(2 * b[0] - 1)
	elif b[0] - b[1] == -1:
		print(2 * b[0] + 1)
	elif b[1] == 0 or b[0] == 0:
		print(-1)
	else:
		if b[1] > b[0]:
			temp = b[0]
			b[0] = b[1]
			b[1] = temp
		if (b[0] - b[1]) % 2 == 0:
			print(2 * (b[0] - 1) + 2)
		else:
			print(2 * (b[0] - 1) + 1)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n == 1 and m == 2 or (n == 2 and m == 1):
		print(1)
	elif n == 1 or m == 1:
		print(-1)
	else:
		maximum = max(n, m)
		minimum = min(n, m)
		x = 0
		if (maximum - minimum) % 2 == 1:
			x = (minimum - 1) * 2 + (maximum - minimum) * 2 - 1
		else:
			x = (minimum - 1) * 2 + (maximum - minimum) * 2
		print(str(x))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n < m:
		(n, m) = (m, n)
	if m == 1 and n > 2:
		print(-1)
		continue
	count = 2 * (m - 1)
	count += 4 * ((n - m) // 2)
	if (n - m) % 2 == 1:
		count += 1
	print(count)


cases = int(input())
for case in range(cases):
	(n, m) = map(int, input().split())
	if n < m:
		(n, m) = (m, n)
	if m == 1:
		if n == 1:
			print(0)
		elif n == 2:
			print(1)
		else:
			print(-1)
	else:
		output = 2 * (m - 1)
		left = n - m
		needed = 1
		output += (left + 1) // 2
		output += 3 * (left - (left + 1) // 2)
		print(output)


problems = int(input())
for problem_no in range(problems):
	line = str(input())
	(n, m) = [int(n) for n in line.split(' ')]
	n -= 1
	m -= 1
	ans = None
	if n > 1 and m == 0 or (n == 0 and m > 1):
		ans = -1
	else:
		ans = max(n, m) * 2
		if (n - m) % 2 == 1:
			ans -= 1
	to_print = str(ans)
	print(to_print)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m > 2 or (m == 1 and n > 2):
		print(-1)
	elif (max(n, m) - min(n, m)) % 2 == 0:
		print((max(n, m) - 1) * 2)
	else:
		print((max(n, m) - 1) * 2 - 1)


n = int(input())
for i in range(n):
	temp = input().split(' ')[:2]
	a = int(temp[0])
	b = int(temp[1])
	if a == 1 and b >= 3:
		print('-1')
	elif b == 1 and a >= 3:
		print('-1')
	elif abs(a - b) % 2 == 0:
		print((min(a, b) - 1) * 2 + abs(a - b) * 2)
	else:
		print((min(a, b) - 1) * 2 + (abs(a - b) - 1) * 2 + 1)


t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	res = 0
	(max_size, min_size) = (max(n, m), min(n, m))
	if n == 1 or m == 1:
		if max_size >= 3:
			print(-1)
		else:
			print(max_size - 1)
	elif (max_size - min_size) % 2 == 0:
		print((max_size - 1) * 2)
	else:
		print((max_size - 1) * 2 - 1)


for t in range(int(input())):
	(x, y) = map(int, input().split())
	diff = abs(x - y)
	if (x == 1 or y == 1) and (x > 2 or y > 2):
		print(-1)
	else:
		print((min(x, y) - 1) * 2 + diff // 2 * 4 + (diff % 2 != 0))


import math
for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n == m:
		print(n + m - 2)
	elif n == 1 and m == 2 or (n == 2 and m == 1):
		print(1)
	elif n == 1 and m > 2 or (n > 2 and m == 1):
		print(-1)
	elif m % 2 == 1 and n % 2 != 1 or (m % 2 != 1 and n % 2 == 1):
		print(min(m, n) * 2 - 2 + abs(m - n) * 2 - 1)
	else:
		print(min(m, n) * 2 - 2 + abs(m - n) * 2)


def solve():
	(n, m) = map(int, input().split())
	if n == 1 and m > 2:
		return -1
	if m == 1 and n > 2:
		return -1
	doKota = 2 * min(m, n) - 2
	ostanek = max(m, n) - min(m, n)
	if ostanek % 2 == 0:
		doKota += ostanek * 2
	else:
		doKota += (ostanek - 1) * 2 + 1
	return doKota
t = int(input())
while t > 0:
	print(solve())
	t -= 1


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ans = 0
	if (n == 1 or m == 1) and abs(m - n) > 1:
		print(-1)
		continue
	if n == m:
		ans += (n - 1) * 2
	elif n > m:
		ans += (m - 1) * 2 + (n - m) // 2 * 4 + (n - m) % 2
	else:
		ans += (n - 1) * 2 + (m - n) // 2 * 4 + (m - n) % 2
	print(ans)


t = int(input())

def q(n, m):
	if n * m < 3:
		return n * m - 1
	elif n == 1 or m == 1:
		return -1
	else:
		v = abs(m - n) // 2 * 2
		v += m + n - 2
		return v
for i in range(t):
	a = input().split()
	(n, m) = (int(a[0]), int(a[1]))
	print(q(n, m))


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m - n >= 2 or (m == 1 and n - m >= 2):
		print(-1)
	elif (n + m) % 2 == 1:
		print(max(n, m) * 2 - 3)
	else:
		print(max(n, m) * 2 - 2)


import sys, math
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a == 1 and b == 1:
		print(0)
	elif a == 1 and b == 2 or (a == 2 and b == 1):
		print(1)
	elif a == 1 or b == 1:
		print(-1)
	elif a == b:
		print(2 * (a - 1))
	elif abs(b - a) % 2 == 0:
		print(2 * min(a - 1, b - 1) + 2 * abs(b - a))
	else:
		print(2 * min(a - 1, b - 1) + 2 * (abs(b - a) - 1) + 1)


t = int(input())
while t > 0:
	(n, m) = [int(x) for x in input().split()]
	if n == 1 and m == 1:
		print(0)
	elif n == 1 and m == 2 or (n == 2 and m == 1):
		print(1)
	elif n == 1 or m == 1:
		print(-1)
	else:
		n = n - 1
		m = m - 1
		lar = m if m > n else n
		small = m + n - lar
		if (lar - small) % 2 == 1:
			print(2 * small + 2 * (lar - small - 1) + 1)
		else:
			print(2 * small + 2 * (lar - small))
	t = t - 1


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a == b == 1:
		print(0)
	elif a + b == 3:
		print(1)
	elif a > 1 and b > 1:
		if (a + b) % 2 == 0:
			print(a - 1 + b - 1 + abs(a - b))
		else:
			print(a - 1 + b - 2 + abs(a - b))
	else:
		print(-1)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	c = (min(n, m) - 1) * 2
	(x, y) = (min(n, m), min(n, m))
	if x == n and y == m:
		print(c)
		continue
	if x == n - 1 and y == m or (x == n and y == m - 1):
		print(c + 1)
		continue
	if x == 1 or y == 1:
		print(-1)
		continue
	add = 1
	go = max(n, m) - x
	if go % 2 == 0:
		c = c + go * 2
	else:
		c = c + (go - 1) * 2 + 1
	print(c)


t = int(input())
ans = list()
for i in range(0, t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		if m == 1:
			ans.append(0)
		elif m == 2:
			ans.append(1)
		else:
			ans.append(-1)
		continue
	if m == 1:
		if n == 2:
			ans.append(1)
		else:
			ans.append(-1)
		continue
	if m < n:
		b = 0
		b += 2 * (m - 1)
		n -= m
		b += 4 * (n // 2)
		if n % 2 == 0:
			ans.append(b)
		elif n % 2 == 1:
			ans.append(b + 1)
	elif n < m:
		b = 0
		b += 2 * (n - 1)
		m -= n
		b += 4 * (m // 2)
		if m % 2 == 0:
			ans.append(b)
		elif m % 2 == 1:
			ans.append(b + 1)
	else:
		ans.append(m + n - 2)
for s in ans:
	print(s)


def dunc1(x, y):
	c = x % 2
	d = y % 2
	if c == d:
		return True
	else:
		return False
for _ in range(int(input())):
	(m, n) = input().split()
	m = int(m)
	n = int(n)
	f = m - 1
	g = n - 1
	if (f == 0 or g == 0) and abs(f - g) > 1:
		print('-1')
		continue
	if dunc1(f, g) is True:
		print(f + g + abs(f - g))
	if dunc1(f, g) is False:
		print(f + g + abs(f - g) - 1)


for t in range(int(input())):
	(r, c) = map(int, input().split())
	do = r - 1
	ri = c - 1
	if abs(do - ri) <= 1:
		print(do + ri)
	elif do == 0 or ri == 0:
		print(-1)
	elif do > ri:
		x = do - ri
		if x % 2 == 0:
			print(do + ri + x)
		else:
			print(do + ri + x - 1)
	elif ri > do:
		x = ri - do
		if x % 2 == 0:
			print(do + ri + x)
		else:
			print(do + ri + x - 1)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a == 1 or b == 1:
		if a == 1 and b == 1:
			print(0)
		elif a == 2 or b == 2:
			print(1)
		else:
			print(-1)
	elif (a - 1) % 2 == 0 and (b - 1) % 2 == 0:
		c = max(a - 1, b - 1)
		print(2 * c)
	elif (a - 1) % 2 == 1 and (b - 1) % 2 == 1:
		c = max(a - 1, b - 1)
		print(2 * c)
	else:
		c = max(a - 1, b - 1)
		print(2 * c - 1)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(a, b) = map(int, input().split())
	if a == 1 and b > 2 or (a > 2 and b == 1):
		print(-1)
		continue
	a -= 1
	b -= 1
	cnt = 0
	if (a + b) % 2 == 1:
		cnt += max(a, b) * 2 - 1
	else:
		cnt += max(a, b) * 2
	print(cnt)


import sys
input = sys.stdin.readline

def ri():
	return int(input())

def rf():
	return list(map(float, input().split()))

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

def rs():
	return input().rstrip()

def out_list(x):
	return ' '.join(map(str, x))

def solve_case():
	(n, m) = rl()
	n -= 1
	m -= 1
	if n == m == 0:
		print(0)
		return
	if (n, m) == (0, 1) or (n, m) == (1, 0):
		print(1)
		return
	if n == 0 or m == 0:
		print(-1)
		return
	if n == m:
		print(n + m)
		return
	mn = min(n, m)
	mx = max(n, m)
	res = mn + 1 + mn
	rem = mx - (mn + 1)
	res += rem * 2 + rem % 2
	print(res)
T = ri()
for ti in range(1, T + 1):
	solve_case()


for _ in range(int(input())):
	(rrrr, clll) = map(int, input().split())
	if rrrr == 1 and clll > 2:
		print(-1)
	elif clll == 1 and rrrr > 2:
		print(-1)
	elif rrrr == clll:
		print(rrrr + (rrrr - 2))
	elif rrrr > clll:
		print(clll + (clll - 2) - (rrrr - clll) % 2 + (rrrr - clll) * 2)
	else:
		print(rrrr + (rrrr - 2) - (clll - rrrr) % 2 + (clll - rrrr) * 2)


t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split(' ')))
	if n == 1 and m == 2 or (n == 2 and m == 1):
		print(1)
		continue
	elif n == 1 and m != 1 or (n != 1 and m == 1):
		print(-1)
		continue
	elif n == 1 and m == 1:
		print(0)
		continue
	else:
		ans = n - 1 + (m - 1)
		if n != m:
			ans += abs(n - m)
		if n % 2 == m % 2:
			pass
		else:
			ans -= 1
		print(ans)


def read_input():
	t = int(input())
	for _ in range(t):
		(n, m) = [int(i) for i in input().split(' ')]
		print(direction(n, m))

def direction(n, m):
	if n == m:
		return 2 * (n - 1)
	rows = max(n, m)
	cols = min(n, m)
	if cols == 1:
		if rows == 2:
			return 1
		else:
			return -1
	square = 2 * (cols - 1)
	left = rows - cols
	snake = 2 * left
	if left % 2 != 0:
		snake -= 1
	return square + snake
read_input()


for s in [*open(0)][1:]:
	(n, m) = sorted(map(int, s.split()))
	print((2 * m - (m - n & 1) - 2, -1)[n < 2 < m])


for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if a == 1:
		if b < 3:
			print(b - 1)
		else:
			print(-1)
	else:
		k = b - a
		if k == 0:
			print(a + a - 2)
		elif k % 2 == 0:
			print(b + b - 2)
		else:
			print(2 * b - 3)


def solve(n, m):
	if n == 1 and m >= 3 or (n >= 3 and m == 1):
		return -1
	wasted = 0
	if abs(m - n) >= 2:
		wasted = abs(m - n)
	if wasted % 2 == 1:
		wasted -= 1
	return n - 1 + m - 1 + wasted

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = map(int, input().split())
		ans = solve(n, m)
		print(ans)
main()


t = int(input())

def solution():
	(n, m) = map(int, input().split())
	if n < m:
		(n, m) = (m, n)
	if n == 1 and m == 1:
		return 0
	if m == 1 and n > 2:
		return -1
	res = n - 1
	if m & 1 and (not n & 1):
		res += n - 2
	if m & 1 and n & 1:
		res += n - 1
	if not m & 1 and n & 1:
		res += n - 2
	if not m & 1 and (not n & 1):
		res += n - 1
	return res
while t:
	print(solution())
	t -= 1


for _ in range(int(input())):
	(n, m) = sorted(list(map(int, input().split())))
	if n == 1 and m > 2:
		print(-1)
	else:
		ex = max(m, n) - min(m, n)
		ans = ex * 2 + 2 * (min(n, m) - 1)
		if ex % 2:
			ans -= 1
		print(ans)


import math

def solve(n, m):
	if n == 1 and m > 2:
		print(-1)
	elif m == 1 and n > 2:
		print(-1)
	else:
		x = min(n, m)
		ans = 2 * x - 2
		rem = n - x + m - x
		if rem % 2 == 0:
			print(ans + rem * 2)
		else:
			print(ans + rem * 2 - 1)
t = int(input())
for test in range(t):
	(n, m) = map(int, input().split())
	solve(n, m)


def solution():
	inp = input().split(' ')
	n = int(inp[0])
	m = int(inp[1])
	if n == 1 or m == 1:
		if n == 2 or m == 2:
			return 1
		elif n == 1 and m == 1:
			return 0
		else:
			return -1
	elif n == m:
		return n + m - 2
	else:
		diff = max(0, max(n, m) - min(n, m) - 1)
		additional_steps = diff // 2 * 4 + diff % 2 * 3
		return min(n, m) * 2 - 1 + additional_steps
count = int(input())
for _ in range(count):
	print(solution())


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n > m:
		(n, m) = (m, n)
	u = 1
	if (m - n) % 2 == 0:
		u = 0
	h = n * 2 - 2 + (m - n) * 2 - u
	if n == 1 and m > 2:
		print(-1)
	else:
		print(h)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n - 1 == 0 and m - 1 > 1 or (m - 1 == 0 and n - 1 > 1):
		print(-1)
	elif n - 1 == 0 and m - 1 == 0:
		print(0)
	elif n - 1 == 0 and m - 1 == 1 or (m - 1 == 0 and n - 1 == 1):
		print(1)
	elif n == m:
		print((n - 1) * 2)
	else:
		cnt = 0
		x = min(n, m)
		y = max(n, m)
		cnt += (x - 1) * 2
		if (y - x) % 2 == 0:
			cnt += (y - x) * 2
		else:
			cnt += (y - x) * 2 - 1
		print(cnt)


t = int(input())
for i in range(t):
	(x, y) = input().split()
	x = int(x)
	y = int(y)
	if x == y:
		print(2 * x - 2)
	elif x == 1 and abs(x - y) > 1:
		print(-1)
	elif y == 1 and abs(x - y) > 1:
		print(-1)
	else:
		a = abs(x - y)
		if a % 2 == 0:
			b = min(x, y)
			print(2 * (b - 1) + 2 * a)
		else:
			b = min(x, y)
			print(2 * (b - 1) + 2 * a - 1)


def count_steps(n, m):
	if m == 1:
		if n == 1:
			return 0
		elif n == 2:
			return 1
		else:
			return -1
	if n == 1:
		if m == 1:
			return 0
		elif m == 2:
			return 1
		else:
			return -1
	min_len = min(n, m)
	max_len = max(n, m)
	stairway_steps = (min_len - 1) * 2
	remainder = max_len - min_len
	if remainder > 0:
		add = 2 * remainder
		if remainder % 2:
			add -= 1
		stairway_steps += add
	return stairway_steps
tests_num = int(input())
for _ in range(tests_num):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	print(count_steps(n, m))


p = int(input())
for i in range(p):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n < m:
		steps2 = (n - 1) * 2
		steps3 = steps2 + (m - n) // 2 * 4 + (m - n) % 2
		if steps2 == 0 and steps3 > 1:
			print(-1)
		else:
			print(steps3)
	else:
		steps2 = (m - 1) * 2
		steps3 = steps2 + (n - m) // 2 * 4 + (n - m) % 2
		if steps2 == 0 and steps3 > 1:
			print(-1)
		else:
			print(steps3)


from collections import Counter
for t in range(int(input())):
	(n, m) = [int(i) for i in input().split()]
	(n, m) = (min(n, m), max(n, m))
	if n == 1 and m > 2:
		print(-1)
	else:
		x = m - 1
		if n % 2 == m % 2:
			print(2 * x)
		else:
			print(2 * x - 1)


import sys
input = sys.stdin.readline
from math import ceil, floor, gcd

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

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

def ma():
	return map(int, input().split())

def st():
	return input().rstrip('\n')

def isPrime(n):
	if n == 1:
		return 0
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return 0
	return 1
t = inp()
for _ in range(t):
	(n, m) = ma()
	if n == 1:
		if m >= 3:
			print(-1)
			continue
		if m == 1:
			print(0)
		else:
			print(1)
		continue
	if m == 1:
		if n >= 3:
			print(-1)
			continue
		if n == 1:
			print(0)
		else:
			print(1)
		continue
	(n, m) = (max(n, m), min(n, m))
	if n == m:
		print(m * 2 - 2)
	else:
		now = m * 2 - 2
		k = n - m - 1
		fac = 0
		if k % 2 == 1:
			fac = 1
		print(now + k * 2 + 1 + fac)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	maxi = max(n, m)
	mini = min(n, m)
	smooth = mini
	rem = max(0, maxi - smooth)
	result = (maxi - 1) * 2
	if rem % 2 == 1:
		result -= 1
	if mini == 1 and maxi > 2:
		print(-1)
	else:
		print(result)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 and m > 2:
		print(-1)
	elif m == 1 and n > 2:
		print(-1)
	elif n == 1:
		print(m - 1)
	elif m == 1:
		print(n - 1)
	else:
		ans = (min(n, m) - 2) * 2
		t = max(n, m) - ans // 2
		ans += (t // 2 - 1) * 4 + 2 + t % 2
		print(ans)


t = int(input())
ans = []
for x in range(t):
	inp = list(map(int, input().split()))
	n = inp[0]
	m = inp[1]
	moves = 0
	if m == 1 and n == 1:
		moves = 0
		ans.append(moves)
		continue
	if m * n == 2:
		moves = 1
		ans.append(moves)
		continue
	if m == 1 or n == 1:
		moves = -1
		ans.append(moves)
		continue
	if m > n:
		if (m - n) % 2 == 0:
			moves = 2 * (n - 1) + (m - n) * 2
		else:
			moves = 2 * (n - 1) + (m - n) * 2 - 1
	elif m < n:
		if (m - n) % 2 == 0:
			moves = 2 * (m - 1) + (n - m) * 2
		else:
			moves = 2 * (m - 1) + (n - m) * 2 - 1
	else:
		moves = 2 * (n - 1)
	ans.append(moves)
for a in ans:
	print(a)


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	if m <= n:
		(m, n) = (n, m)
	if n == 1 and m > 2:
		print(-1)
		continue
	odd = m + n & 1
	if not odd:
		res = 2 * m - 2
		print(res)
	else:
		res = 2 * m - 3
		print(res)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		if n + m > 3:
			print(-1)
			continue
	maxi = (max(n, m) - 1) * 2
	maxi -= 1 if n % 2 != m % 2 else 0
	print(maxi)


def solve(m, n):
	if m < n:
		(m, n) = (n, m)
	if 1 in [m, n] and max(m, n) > 2:
		return -1
	if (m == 2 or n == 2) and (m == 1 or n == 1):
		return 1
	base = 2 * (n - 1)
	base += (m - n) // 2
	base += 3 * (m - n) // 2
	return base
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	print(solve(n, m))


n = int(input())
for i in range(n):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
		continue
	if n == 1 and m == 2 or (m == 1 and n == 2):
		print(1)
		continue
	if n == 1 and m > 2 or (m == 1 and n > 2):
		print(-1)
		continue
	x = min(n, m)
	y = max(n, m)
	if x % 2 != 0 and y % 2 != 0:
		print((y - 1) * 2)
	elif x % 2 != 0 or y % 2 != 0:
		print((y - 1) * 2 - 1)
	else:
		print((y - 1) * 2)


test = int(input())
for t in range(test):
	(m, n) = map(int, input().split())
	a = min(m, n)
	b = max(m, n)
	if a == 1 and b > 2:
		print(-1)
	elif a * b == 1:
		print(0)
	else:
		print((a - 1) * 2 + (b - a) * 2 - (b - a) % 2)


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if m == 1 and n == 1:
		print(0)
	elif n == 1 and m > 2 or (m == 1 and n > 2):
		print(-1)
	elif m == n:
		print(m + n - 2)
	else:
		count = 0
		min_ = min(m, n)
		max_ = max(m, n)
		count += min_ * 2 - 2
		max_ = max_ - min_ + 1
		if max_ % 2 == 1:
			count += 2 * max_ - 2
		else:
			count += 2 * max_ - 3
		print(count)


def solve():
	for tc in range(int(input())):
		(a, b) = list(map(int, input().split()))
		if a == 1 and b - 1 > 1 or (b == 1 and a - 1 > 1):
			res = -1
		elif a % 2 == b % 2:
			res = 2 * max(a - 1, b - 1)
		else:
			res = min(2 * max(a - 1, b - 2), 2 * max(a - 2, b - 1)) + 1
		print(res)
solve()


import math
x = int(input())
while x > 0:
	inp = input()
	q = list(map(int, inp.split(' ')))
	q.sort()
	n = q[0]
	m = q[1]
	if n == 1:
		if m == 1:
			print(0)
		elif m == 2:
			print(1)
		else:
			print(-1)
	elif m - n == 1:
		print(n + m - 2)
	else:
		ans = n + n - 2
		sub = (m - n) % 2
		sub2 = (m - n) // 2
		ans += sub2 * 3
		ans += sub2
		ans += sub
		print(ans)
	x -= 1


t = int(input())
for it in range(t):
	nm = input()
	n = int(nm.split(' ')[0])
	m = int(nm.split(' ')[1])
	a = n - 1
	b = m - 1
	diff = a + b
	if diff == 0:
		print('0')
	elif diff == 1:
		print('1')
	elif diff > 1 and (a == 0 or b == 0):
		print('-1')
	else:
		ans = 0
		if a < b:
			min = a
			rd = b - a
		else:
			min = b
			rd = a - b
		ans = min * 2
		if rd % 2 == 0:
			ans = ans + 2 * rd
		else:
			ans = ans + 2 * rd - 1
		print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	x = max(a, b)
	y = min(a, b)
	if a == 1 and b == 1:
		print(0)
	elif a == 1 and b > 2:
		print(-1)
	elif b == 1 and a > 2:
		print(-1)
	elif a == b:
		print((a - 1) * 2)
	elif a % 2 == 0 and b % 2 == 0 or (a % 2 != 0 and b % 2 != 0):
		print((x - 1) * 2)
	else:
		print((x - 2) * 2 + 1)


from sys import stdin
T = int(stdin.readline())

def solve(n, m):
	if n > m:
		return solve(m, n)
	if n == 1:
		if m == 1:
			return 0
		if m == 2:
			return 1
		return -1
	if n == 2:
		if m % 2 == 0:
			return n * m - 2
		return n * m - 3
	return (n - 2) * 2 + solve(2, m - n + 2)
for t in range(T):
	[n, m] = [int(x) for x in stdin.readline().strip().split(' ')]
	r = solve(n, m)
	print(r)


n = int(input())
lst = []
for _ in range(n):
	(a, b) = map(int, input().split())
	if a == 1 and b == 1:
		lst.append(0)
	elif a == 1 and b > 2 or (b == 1 and a > 2):
		lst.append(-1)
	elif a < b:
		x = (a - 1) * 2
		b -= a
		x += 2 * b - 1
		if b % 2 == 0:
			x += 1
		lst.append(x)
	else:
		x = (b - 1) * 2
		a -= b
		x += 2 * a - 1
		if a % 2 == 0:
			x += 1
		lst.append(x)
for i in lst:
	print(i)


t = int(input())
lst = []
for r in range(t):
	(aa, bb) = input().split()
	n = int(aa)
	m = int(bb)

	def f(n, m):
		if (n - m) % 2 == 0:
			y = int(n - 1 + (m - 1) + (n - m))
		else:
			y = int(n - 1 + (m - 1) + (n - m - 1))
		return y
	if n == 1 and m > 2:
		x = -1
	elif m == 1 and n > 2:
		x = -1
	elif n == m:
		x = int(n - 1 + (m - 1))
	elif n > m:
		x = f(n, m)
	elif m > n:
		x = f(m, n)
	lst.append(x)
for u in lst:
	print(int(u))


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n == 1 and m == 2 or (n == 2 and m == 1):
		print(1)
	elif n < 2 or m < 2:
		print(-1)
	else:
		if n > m:
			cub = 2 * m - 2
		else:
			cub = 2 * n - 2
		delta = abs(m - n)
		if delta % 2 == 0:
			dop_z = delta // 2 * 4
		else:
			dop_z = delta // 2 * 4 + 1
		result = cub + dop_z
		print(result)


t = int(input())
while t > 0:
	(m, n) = map(int, input().split())
	x = min(m, n)
	y = abs(m - n)
	z = 2 * (x - 1) + y // 2 * 4
	if m == 1 and n > 2:
		print(-1)
	elif n == 1 and m > 2:
		print(-1)
	elif y % 2 == 0:
		print(z)
	else:
		print(z + 1)
	t = t - 1


def solve():
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if (n == 1 or m == 1) and abs(n - m) >= 2:
		print(-1)
		return
	minMoves = 2 * min(n, m) - 2
	k = 0
	if abs(n - m) % 2 == 0:
		k = 2 * abs(n - m)
	else:
		k = 2 * abs(n - m) - 1
	print(minMoves + k)
t = int(input())
while t:
	solve()
	t -= 1


def go():
	(n, m) = map(int, input().split())
	if min(m, n) == 1 and max(m, n) > 2:
		print(-1)
		return
	(n, m) = (min(m, n) - 1, max(m, n) - 1)
	print(n * 2 + (m - n) * 2 - (m - n) % 2)
for t in range(int(input())):
	go()


def inp(*, cf=lambda x: x):
	return cf(input())

def inpint():
	return inp(cf=int)

def inp_line(*, cf=lambda x: x, s=' '):
	return list(map(cf, input().split(s)))

def inpints():
	return inp_line(cf=int)
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def test_case(case):
	(n, m) = inpints()
	if n == 1 and m == 1:
		return 0
	if n == 1 and m == 2 or (n == 2 and m == 1):
		return 1
	if n == 1 and m > 2 or (n > 2 and m == 1):
		return -1
	if m > n:
		(n, m) = (m, n)
	a = min(n, m)
	b = max(n, m)
	ans = a - 1 + a - 1
	if n == m:
		return ans
	if n == m + 1:
		return ans + 1
	if (n - m) % 2 == 0:
		return ans + n - m + n - m
	if (n - m) % 2 == 1:
		return ans + n - m + n - m - 1

def main():
	T = inpint()
	for t in range(1, T + 1):
		print(test_case(t))
main()


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a == 1 or b == 1:
		if a == 1 and b == 1:
			print(0)
		elif a == 2 or b == 2:
			print(1)
		else:
			print(-1)
	else:
		if a > b:
			(a, b) = (b, a)
		k = (a - 1) * 2
		if a == b:
			print(k)
		elif a % 2 == 0 and b % 2 == 0:
			print(k + (b - a) * 2)
		elif a % 2 == 0 and b % 2 != 0:
			b = b - 1
			print(1 + k + (b - a) * 2)
		elif a % 2 != 0 and b % 2 != 0:
			print(k + (b - a) * 2)
		else:
			b = b - 1
			print(1 + k + (b - a) * 2)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		if abs(n - m) > 1:
			print(-1)
		else:
			print(abs(n - m))
	else:
		if n >= m:
			maxm = n
			minm = m
		else:
			maxm = m
			minm = n
		if n % 2 == m % 2:
			print(2 * (maxm - 1))
		else:
			ans = 2 * (minm - 1) + 2 * (maxm - minm) - 1
			print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	temp = n - m
	if n - m < 0:
		temp = m - n
	if n == 1 and m > 2 or (n > 2 and m == 1):
		print('-1')
	elif n == 1 and m == 1:
		print('0')
	else:
		mi = min(n, m)
		res = 2 * mi - 2
		if temp == 1:
			res += 1
		elif temp & 1 != 0:
			res += 2 * temp - 1
		else:
			res += 2 * temp
		print(res)


testcases = int(input())
for i in range(testcases):
	(x, y) = [int(x) for x in input().split(' ')]
	if x == 1:
		if y <= 2:
			print(y - 1)
		else:
			print(-1)
	elif y == 1:
		if x <= 2:
			print(x - 1)
		else:
			print(-1)
	else:
		n = min(x, y)
		ans = (n - 1) * 2
		if (max(x, y) - n) % 2 == 0:
			ans += (max(x, y) - n) * 2
		else:
			ans += (max(x, y) - n) * 2 - 1
		print(ans)


for num in range(int(input())):
	user = [int(i) for i in input().split(' ')]
	ans = 0
	if user[0] == 1 and user[1] == 1:
		print(0)
		continue
	elif user[0] == 1:
		if user[1] == 2:
			print(1)
			continue
		else:
			print(-1)
			continue
	elif user[1] == 1:
		if user[0] == 2:
			print(1)
			continue
		else:
			print(-1)
			continue
	elif user[0] < user[1]:
		ans = user[0] * 2 - 2
		temp = user[1] - user[0]
		if temp & 1 == 1:
			if temp == 1:
				ans = ans + 1
			else:
				ans = ans + (temp - 1) * 2 + 1
		else:
			ans = ans + temp * 2
	elif user[1] < user[0]:
		ans = user[1] * 2 - 2
		temp = user[0] - user[1]
		if temp & 1 == 1:
			if temp == 1:
				ans = ans + 1
			else:
				ans = ans + (temp - 1) * 2 + 1
		else:
			ans = ans + temp * 2
	else:
		ans = user[0] * 2 - 2
	print(ans)


import fileinput

def getanswer_rectangle(dx, dy):
	return 2 * dx - (dx - dy) % 2

def getanswer(n, m):
	(deltax, deltay) = (m - 1, n - 1)
	if deltax == 0 and deltay > 1:
		return -1
	if deltax > 1 and deltay == 0:
		return -1
	if deltax >= deltay:
		return getanswer_rectangle(deltax, deltay)
	if deltay > deltax:
		return getanswer_rectangle(deltay, deltax)

def solve():
	ins = []
	for line in fileinput.input():
		ins.append(line.strip('\n'))
	num_tests = int(ins[0])
	test_cases = ins[1:]
	for i in range(num_tests):
		(n, m) = test_cases[i].split(' ')
		answer = getanswer(int(n), int(m))
		print(answer)
solve()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n < m:
		(n, m) = (m, n)
	diag = (m - 1) * 2
	nediag = (n - m - 1) * 2 + (n - m - 1) % 2 + 1
	if m == 1 and n > 2:
		print(-1)
		continue
	print(diag + nediag)


n = int(input())
for j in range(0, n):
	(a, b) = map(int, input().split())
	if a == 1 and b >= 3:
		print(-1)
	elif a >= 3 and b == 1:
		print(-1)
	elif a == 1 and b == 1:
		print(0)
	elif a == 1 or b == 1:
		print(1)
	else:
		mini = min(a, b)
		moves = 2 * (mini - 1)
		need_continous_steps = max(a, b) - mini
		if need_continous_steps % 2 == 0 and need_continous_steps != 0:
			moves += 2 * need_continous_steps
		elif need_continous_steps == 0:
			moves += 0
		else:
			moves += 2 * need_continous_steps - 1
		print(moves)


t = int(input())
for _ in range(t):
	(a, b) = [int(x) for x in input().split()]
	i = 1
	j = 1
	diff = abs(a - b)
	if a - i == 0 and b - j == 0:
		print(0)
	elif a - i > 1 and b - j == 0:
		print(-1)
	elif a - i == 0 and b - j > 1:
		print(-1)
	else:
		temp = a if a < b else b
		ans = temp * 2 - 2
		if diff == 1:
			ans = ans + 1
		elif diff & 1 != 0:
			ans = ans + (2 * diff - 1)
		else:
			ans = ans + 2 * diff
		print(ans)


from math import *

def up(pos):
	return (pos[0] - 1, pos[1])

def down(pos):
	return (pos[0] + 1, pos[1])

def left(pos):
	return (pos[0], pos[1] - 1)

def right(pos):
	return (pos[0], pos[1] + 1)

def solve(n, m):
	local = (1, 1)
	pos = (n, m)
	contador = 0
	if pos[0] == 1 and pos[1] == 1:
		return 0
	if pos[0] == 1 and pos[1] > 2 or (pos[0] > 2 and pos[1] == 1):
		return -1
	if up(pos) == local or down(pos) == local or left(pos) == local or (right(pos) == local):
		return 1
	if n == m + 1 or m == n + 1:
		return n + m - 2
	index = pos.index(min(n, m))
	contador += (pos[index] - 1) * 2
	posicao = max(n, m) - pos[index] + 1
	distancia = dist((posicao, 1), local)
	if distancia == 1:
		distancia = 3
	else:
		distancia = distancia // 2 * 3 + distancia - distancia // 2
	contador += distancia
	return int(contador)
T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	print(solve(n, m))


for i in range(int(input())):
	(n, m) = map(int, input().split())
	a = max(n, m)
	b = min(n, m)
	if b == 1 and a > 2:
		print(-1)
	else:
		c = 2 * (b - 1) + 2 * (a - b)
		if (a + b) % 2 == 1:
			print(c - 1)
		else:
			print(c)


for i in range(int(input())):
	(n, m) = map(int, input().split())
	(a, b) = (1, 1)
	s = 0
	j = 0
	if n == 1 and m > 2:
		print('-1')
		continue
	elif m == 1 and n > 2:
		print('-1')
		continue
	if m >= n:
		a = n
		b = n
		s = n * 2 - 2
	else:
		a = m
		b = m
		s = m * 2 - 2
	if a == n and b == m:
		print(s)
	else:
		if a == n:
			if (m - b) % 2 == 1 and m - b != 1:
				s = s + (m - b) * 2 - 1
			elif (m - b) % 2 == 0 and m - b != 1:
				s = s + (m - b) * 2
			else:
				s = s + 1
		elif (n - a) % 2 == 1 and n - a != 1:
			s = s + (n - a) * 2 - 1
		elif (n - a) % 2 == 0 and n - a != 1:
			s = s + (n - a) * 2
		else:
			s = s + 1
		print(s)


test_case = int(input())
for _ in range(test_case):
	(r, c) = list(map(int, input().split()))
	if r == 1 and c == 1:
		print(0)
		continue
	elif r == 1 and c > 2 or (c == 1 and r > 2):
		print(-1)
		continue
	else:
		Max = max(r, c)
		Min = min(r, c)
		if (Max - Min) % 2 == 1:
			print(2 * Max - 3)
		else:
			print(2 * Max - 2)


test = int(input())
for i in range(test):
	size = list(map(int, input().split()))
	n = min(size) - 1
	m = max(size) - 1
	move = n * 2
	ext = (m - n) // 2 + (m - n) % 2 + 3 * ((m - n) // 2)
	if n == 0 and m > 1:
		print(-1)
	else:
		print(move + ext)


def solve():
	(n, m) = [int(i) for i in input().split()]
	if n == 1 and m == 1:
		print(0)
		return
	if (n == 1 or m == 1) and max(m, n) - min(m, n) > 1:
		print(-1)
		return
	k = (min(m, n) - 1) * 2
	if m == n:
		print(k)
	else:
		l = max(m, n) - min(m, n)
		if l % 2 == 0:
			k += l * 2
		else:
			k += (l - 1) * 2 + 1
		print(k)

def main():
	t = int(input())
	while t:
		solve()
		t -= 1
	return
main()


from sys import stdin
from math import log, floor, ceil, gcd
from collections import defaultdict as dd
input = stdin.readline
inp = lambda : int(stdin.readline())
rs = lambda : stdin.readline().strip()

def ra(typ):
	return list(map(typ, stdin.readline().split()))

def rv(typ):
	return map(typ, stdin.readline().split())

def main():
	for _ in range(inp()):
		x = sorted(ra(int))
		if x[0] == 1:
			if x[1] == 1:
				print(0)
			elif x[1] == 2:
				print(1)
			else:
				print(-1)
		else:
			n = (x[0] - 1) * 2
			x[1] -= x[0]
			if x[1] % 2 == 0:
				n += x[1] * 2
			else:
				n += x[1] * 2 - 1
			print(n)
main()


n = int(input())
for i in range(n):
	(bar, kol) = list(map(int, input().split()))
	temp = bar - kol
	if temp < 0:
		temp = kol - bar
	if bar == 1 and kol > 2 or (bar > 2 and kol == 1):
		print(-1)
	elif bar == 1 and kol == 1:
		print(0)
	else:
		mini = min(bar, kol)
		hasil = 2 * mini - 2
		if temp == 1:
			hasil += 1
		elif temp % 2 != 0:
			hasil += 2 * temp - 1
		else:
			hasil += 2 * temp
		print(hasil)


import sys

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

def get_ls():
	return list(map(int, sys.stdin.readline().strip().split()))

def main():
	for _ in range(int(input())):
		(n, m) = get_ints()
		if n < m:
			(n, m) = (m, n)
		if m == 1 and n > 2:
			print(-1)
		else:
			print(n * 2 - 2 - (n + m) % 2)
main()