Read problems statements in Mandarin Chinese  and Russian. 

Recently Chef bought a bunch of robot-waiters. And now he needs to know how much to pay for the electricity that robots use for their work. All waiters serve food from the kitchen (which is in the point (0, 0)) and carry it to some table (which is in some point (x, y)) in a shortest way. But this is a beta version of robots and they can only do the next moves: turn right and make a step forward or turn left and make a step forward. Initially they look in direction of X-axis. Your task is to calculate for each query the number of moves they’ll do to reach corresponding table.

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

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. For each test case there is a sing line containing two space-separated integers - x and y.

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

For each test case, output a single line containing number of moves that robot will make to reach point (x, y)

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

$1 ≤ T ≤ 10^{5}$
$-10^{9} ≤ x, y ≤ 10^{9}$
 
----- Sample Input 1 ------ 
2
3 3
3 4
----- Sample Output 1 ------ 
6
7

------ Explanation 0 ------ 

Example case 1. Sequence of moves would be LRLRLR

Difficulty: MEDIUM
Tags: Constructive algorithms

Mathematics

Ad-hoc

Starter code:

Solutions:
t = int(input())
while t > 0:
	(x, y) = input().split()
	x = int(x)
	y = int(y)
	x = abs(x)
	y = abs(y)
	z = min(x, y)
	x = x - z
	y = y - z
	if x == 0:
		if y % 2 != 0:
			res = 2 * y - 1
		else:
			res = 2 * y
	elif x % 2 != 0:
		res = 2 * x + 1
	else:
		res = 2 * x
	print(2 * z + res)
	t = t - 1


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	(x, y) = (abs(x), abs(y))
	count = 0
	if x != y and y > x:
		count += 2 * x
		if y - x == 1:
			count += 1
		elif (y - x) / 2 == (y - x) // 2:
			count += (y - x) // 2 * 4
		else:
			count += (y - x - 1) // 2 * 4 + 1
	elif x != y and x > y:
		count += 2 * y
		if x - y == 1:
			count += 3
		elif (x - y) / 2 == (x - y) // 2:
			count += (x - y) // 2 * 4
		else:
			count += (x - y - 1) // 2 * 4 + 3
	else:
		count += 2 * x
	print(count)


for i in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	steps = 0
	if x >= y:
		steps = 2 * y
		t = (x - y) % 2 * 3 + (x - y) // 2 * 4
	else:
		steps = 2 * x
		t = (y - x) % 2 * 1 + (y - x) // 2 * 4
	print(steps + t)


T = int(input())
for i in range(T):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	z = x + y
	if x == y:
		print(x * 2)
	elif x > y:
		if z % 2 == 0:
			print(x * 2)
		else:
			print(x * 2 + 1)
	elif z % 2 == 0:
		print(y * 2)
	else:
		print(y * 2 - 1)


T = int(input())
for _ in range(T):
	(N, M) = map(int, input().split())
	(A, B) = (abs(N), abs(M))
	C = min(A, B)
	A -= C
	B -= C
	ans = 2 * C
	if A > 0:
		if A % 2 == 0:
			ans += 2 * A
		else:
			ans += 2 * A + 1
	elif B % 2 == 0:
		ans += 2 * B
	else:
		ans += 2 * B - 1
	print(ans)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	new_x = abs(x)
	new_y = abs(y)
	var = min(new_x, new_y)
	count = 2 * var
	new_x -= var
	new_y -= var
	if new_x != 0:
		if new_x % 2 == 0:
			count += 2 * new_x
		else:
			count += 2 * new_x + 1
	elif new_y % 2 == 0:
		count += 2 * new_y
	else:
		count += 2 * new_y - 1
	print(count)


t = int(input())
for _ in range(t):
	l = list(map(int, input().split()))
	for i in range(2):
		l[i] = abs(l[i])
	if l[1] - l[0] == 1 or l[1] == l[0]:
		print(l[0] + l[1])
	else:
		cnt = 0
		if l[0] < l[1]:
			cnt += 2 * l[0]
			if (l[1] - l[0]) % 2 == 0:
				cnt += 2 * (l[1] - l[0])
			else:
				cnt += 2 * (l[1] - l[0]) - 1
		else:
			cnt += 2 * l[1]
			if (l[0] - l[1]) % 2 == 0:
				cnt += 2 * (l[0] - l[1])
			else:
				cnt += 2 * (l[0] - l[1]) + 1
		print(cnt)


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	k = min(x, y)
	mov = k * 2
	if x > y:
		if (x - k) % 2 == 0:
			mov += 2 * (x - k)
		elif (x - k) % 2 != 0:
			mov += 2 * (x - k) + 1
	elif x < y:
		if (y - k) % 2 == 0:
			mov += 2 * (y - k)
		elif (y - k) % 2 != 0:
			mov += 2 * (y - k) - 1
	print(mov)


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	count = 0
	x = abs(x)
	y = abs(y)
	if x == y:
		count = x + y
	elif x > y:
		x = x - y
		count = count + 2 * y
		if x % 2 != 0:
			num = int(x / 2) + 1
			count = count + 4 * num - 1
		else:
			num = int(x / 2)
			count = count + 4 * num
	else:
		y = y - x
		count = count + 2 * x
		if y % 2 != 0:
			num = int(y / 2)
			count = count + 4 * num + 1
		else:
			num = int(y / 2)
			count = count + 4 * num
	print(count)


test = int(input())
for _ in range(test):
	(x, y) = map(int, input().split(' '))
	(a, b) = (abs(x), abs(y))
	if b > a:
		c = b - a
		print(2 * a + 4 * int(c / 2) + c % 2)
	elif a > b:
		c = a - b
		print(2 * b + 4 * int(c / 2) + c % 2 * 3)
	else:
		print(2 * a)


t = int(input())
for _ in range(t):
	(x, y) = list(map(int, input().strip().split()))
	x = abs(x)
	y = abs(y)
	if x <= y:
		if (x + y) % 2 == 0:
			print(2 * y)
		else:
			print(2 * y - 1)
	elif (x + y) % 2 == 0:
		print(2 * x)
	else:
		print(2 * x + 1)


tests = int(input())
for i in range(tests):
	(x, y) = map(int, input().split())
	(x, y) = (abs(x), abs(y))
	ans = min(x, y) * 2
	if y - x > 0:
		diff = y - x
		ans += 2 * diff
		if diff & 1:
			ans -= 1
	if x - y > 0:
		diff = x - y
		ans += 2 * diff
		if diff & 1:
			ans += 1
	print(ans)


import sys, math, numpy as np
from sys import stdin, stdout
mod = int(1000000000.0 + 7)
from statistics import mode
from collections import deque, defaultdict
from math import ceil, floor, inf, factorial, gcd, log2
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()

def lcm(a, b):
	return a * b // gcd(a, b)

def setbits(n):
	return bin(n).count('1')

def resetbits(n):
	return bin(n).count('0')

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr_p(n, r, p):
	(num, den) = (1, 1)
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * modinv(den, p) % p

def isPrime(num):
	if num <= 1:
		return False
	if num == 2 or n == 3:
		return True
	if num % 2 == 0 or num % 3 == 0:
		return False
	for i in range(5, int(num ** 0.5) + 1, 6):
		if num % i == 0 or num % (i + 2) == 0:
			return False
	return True

def bin_search(arr, low, high, val):
	while low <= high:
		mid = low + (high - low) // 2
		if arr[mid] == val:
			return mid
		elif arr[mid] < val:
			low = mid + 1
		else:
			high = mid - 1
	return -1

def sumofdigit(num):
	count = 0
	while num:
		count += num % 10
		num //= 10
	return count

def make_dic(arr):
	freq = {}
	for val in arr:
		freq[val] = freq[val] + 1 if val in freq else 1
	return freq

def inputmatrix():
	(r, c) = iia()
	mat = [0] * r
	for i in range(r):
		mat[i] = iia()
	return mat

def prefix_sum(n, arr):
	for i in range(1, n):
		arr[i] += arr[i - 1]
	return arr

def binomial(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def divisors(n):
	res = []
	for i in range(1, ceil(sqrt(n)) + 1):
		if n % i == 0:
			if i == n // i:
				res.append(i)
			else:
				res.append(i)
				res.append(n // i)
	return res
for _ in range(ii1()):
	(n, m) = iia()
	if abs(n) == abs(m):
		print(abs(n) + abs(m))
		continue
	elif abs(n) > abs(m):
		if abs(n) - abs(m) & 1:
			print(2 * abs(n) + 1)
		else:
			print(2 * abs(n))
	elif abs(m) - abs(n) & 1:
		print(2 * abs(m) - 1)
	else:
		print(2 * abs(m))


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


for t in range(int(input())):
	temp = list(map(int, input().split()))
	a = abs(temp[0])
	b = abs(temp[1])
	if a == b:
		print(2 * a)
	else:
		ans = 0
		if a < b:
			ans += 2 * a
			b -= a
			if b % 2 == 0:
				ans += 2 * b
			else:
				ans += 2 * b - 1
		else:
			ans += 2 * b
			a -= b
			if a % 2 == 0:
				ans += 2 * a
			else:
				ans += 2 * a + 1
		print(ans)


t = int(input())
for T in range(t):
	(x, y) = [int(x) for x in input().split()]
	x = abs(x)
	y = abs(y)
	ans = 0
	if x == y or y == x + 1:
		ans = x + y
	elif x > y:
		ans = 2 * y + 2 * (x - y) + (x - y) % 2
	else:
		ans = 2 * x + 2 * (y - x - 1) + (y - x - 1) % 2 + 1
	print(ans)


def step(a, b):
	if a == 0:
		if b % 2 == 0:
			return 2 * b
		else:
			return 2 * b - 1
	if b == 0:
		if a % 2 == 0:
			return 2 * a
		else:
			return 2 * a + 1
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	if x == y:
		print(2 * x)
		continue
	print(2 * min(x, y) + step(x - min(x, y), y - min(x, y)))


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


for _ in range(int(input())):
	z = input().split()
	(x, y) = map(lambda x: abs(int(x)), z[:2])
	base = x + y
	diff = abs(y - x - base % 2)
	print(base + diff)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split(' '))
	(a, b) = (abs(x), abs(y))
	if b > a:
		c = b - a
		print(2 * a + 4 * int(c / 2) + c % 2)
	elif a > b:
		c = a - b
		print(2 * b + 4 * int(c / 2) + c % 2 * 3)
	else:
		print(2 * a)


t = int(input())
while t > 0:
	xy = str(input())
	(x, y) = xy.split(' ')
	x = abs(int(x))
	y = abs(int(y))
	if y == 0:
		if x % 2 == 0:
			print(2 * x)
		else:
			print(2 * x + 1)
		t -= 1
		continue
	if x == 0:
		if y % 2 == 0:
			print(2 * y)
		else:
			print(2 * y - 1)
		t -= 1
		continue
	if x > y:
		if (x - y) % 2 == 0:
			print(2 * y + 2 * (x - y))
		else:
			print(2 * y + 2 * (x - y) + 1)
		t -= 1
		continue
	if x < y:
		if (y - x) % 2 == 0:
			print(2 * x + 2 * (y - x))
		else:
			print(2 * x + 2 * (y - x) - 1)
		t -= 1
		continue
	print(2 * x)
	t -= 1


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


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


t = int(input())
for c in range(t):
	(x, y) = [int(z) for z in input().split()]
	if x < 0:
		x = -x
	if y < 0:
		y = -y
	ans = x
	if ans < y:
		ans = y
	ans *= 2
	if x < y:
		if (y - x) % 2 == 1:
			ans -= 1
	elif (x - y) % 2 == 1:
		ans += 1
	print(ans)


for i in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	sum = 2 * min(x, y)
	k = min(x, y)
	x -= k
	y -= k
	if x > y:
		if x & 1 == 0:
			sum += 2 * x
		else:
			sum += 2 * x + 1
	elif x < y:
		if y & 1 == 1:
			sum += 2 * y - 1
		else:
			sum += 2 * y
	print(sum)


for i in range(int(input())):
	m = list(input().split(' '))
	x = abs(int(m[0]))
	y = abs(int(m[1]))
	if x > y:
		if (x - y) % 2 != 0:
			print(2 * y + 2 * (x - y) + 1)
		elif (x - y) % 2 == 0:
			print(2 * y + 2 * (x - y))
	elif y > x:
		if (y - x) % 2 != 0:
			print(2 * x + 2 * (y - x) - 1)
		elif (y - x) % 2 == 0:
			print(2 * x + 2 * (y - x))
	elif x == y:
		print(2 * x)


t = 'char'
p = 0
for _ in range(int(input())):
	(n1, n2) = map(int, input().split())
	n1 = abs(n1)
	n2 = abs(n2)
	if n1 == n2:
		print(2 * n1)
	elif n1 > n2:
		if (n1 + n2) % 2 == 0:
			print(2 * n1)
		else:
			print(2 * n1 + 1)
	elif (n1 + n2) % 2 == 0:
		print(2 * n2)
	else:
		print(2 * n2 - 1)


for tc in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	if x == y:
		print(x + y)
	elif x < y:
		if (y - x) % 2 == 0:
			print(2 * y)
		else:
			print(2 * y - 1)
	elif (x - y) % 2 == 0:
		print(2 * x)
	else:
		print(2 * x + 1)


T = int(input())
ans = []
for _ in range(T):
	(x, y) = [int(i) for i in input().split()]
	x = abs(x)
	y = abs(y)
	if x == y:
		s = x * 2
	elif x > y:
		k = x - y
		s = k // 2 * 4 + k % 2 * 3 + (x - k) * 2
	elif y > x:
		k = y - x
		s = k // 2 * 4 + k % 2 * 1 + (y - k) * 2
	ans.append(s)
for i in ans:
	print(i)


t = int(input())
for _ in range(t):
	(x, y) = map(lambda x: abs(int(x)), input().split())
	if x == y:
		print(x + y)
	elif x < y:
		m = y - x
		if m % 2 == 0:
			m *= 2
		else:
			m = (m - 1) * 2 + 1
		print(2 * x + m)
	elif x > y:
		m = x - y
		if m % 2 == 0:
			m *= 2
		else:
			m = (m + 1) * 2 - 1
		print(2 * y + m)


def val(a, b):
	one = 1
	two = 1
	if a & 1:
		one = 2 * a + 1
	else:
		one = 2 * a
	if b & 1:
		two = 2 * b - 1
	else:
		two = 2 * b
	return one + two
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	z = min(x, y)
	print(2 * z + val(x - z, y - z))


t = int(input())
for _ in range(t):
	(x, y) = [int(i) for i in input().split()]
	x = abs(x)
	y = abs(y)
	m = min(x, y)
	x = x - m
	y = y - m
	if x == 0:
		if y % 2 != 0:
			res = 2 * y - 1
		else:
			res = 2 * y
	elif x % 2 != 0:
		res = 2 * x + 1
	else:
		res = 2 * x
	print(2 * m + res)


T = int(input())
for i in range(T):
	(x, y) = list(map(int, input().split()))
	(x, y) = (abs(x), abs(y))
	s = min(x, y)
	x -= s
	y -= s
	if x == 0:
		if y % 2 != 0:
			t = 2 * y - 1
		else:
			t = 2 * y
	elif x % 2 != 0:
		t = 2 * x + 1
	else:
		t = 2 * x
	print(t + 2 * s)


def answer(a, b):
	k = abs(a - b)
	return 2 * k if k % 2 == 0 else 2 * k - (1 if a < b else -1)
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(x, y) = (abs(x), abs(y))
	ans = answer(x, y)
	print(ans + (0 if x == 0 or y == 0 else 2 * min(x, y)))


for _ in range(int(input())):
	(x, y) = map(int, input().strip().split())
	x = abs(x)
	y = abs(y)
	if x == 0 and y == 0:
		print(0)
	elif x == 0 and y != 0:
		if y % 2 == 0:
			print(2 * y)
		else:
			print(2 * y - 1)
	elif y == 0 and x != 0:
		if x % 2 == 0:
			print(2 * x)
		else:
			print(2 * x + 1)
	else:
		lol = min(x, y)
		req = lol * 2
		x = x - lol
		y = y - lol
		if x == 0 and y == 0:
			print(req)
		elif x == 0 and y != 0:
			if y % 2 == 0:
				print(req + 2 * y)
			else:
				print(req + 2 * y - 1)
		elif y == 0 and x != 0:
			if x % 2 == 0:
				print(req + 2 * x)
			else:
				print(req + 2 * x + 1)


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


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	mi = min(x, y)
	ans = mi * 2
	x -= mi
	y -= mi
	if x == 0:
		if y == 0:
			ans += 0
		elif abs(y) % 2 == 0:
			ans += abs(y) * 2
		else:
			ans += abs(y) * 2 - 1
	elif y == 0:
		if abs(x) % 2 == 1:
			ans += abs(x) * 2 + 1
		else:
			ans += abs(x) * 2
	print(ans)


for t in range(int(input())):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	if x == y:
		print(2 * x)
	elif x > y:
		if (x + y) % 2 == 0:
			print(2 * x)
		else:
			print(2 * x + 1)
	elif (x + y) % 2 == 0:
		print(2 * y)
	else:
		print(2 * y - 1)
		(5, 3)
		(3, 5)


t = int(input())
for _ in range(t):
	(x, y) = [int(i) for i in input().split()]
	x = abs(x)
	y = abs(y)
	if x == y:
		print(2 * x)
	else:
		m = min(x, y)
		x1 = m
		y1 = m
		if y - y1 != 0:
			t = y - y1
			mul = t * 2
			rem = t % 2
			ans = m * 2 + (mul - rem)
			print(ans)
		else:
			t = x - x1
			mul = t * 2
			rem = t % 2
			if rem:
				mul += 1
			ans = m * 2 + mul
			print(ans)


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


import math
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	x = abs(x)
	y = abs(y)
	if x == y:
		print(x + y)
	else:
		z = min(x, y)
		s = 2 * z
		if y > x:
			c = y - x
			s = s + int(math.ceil(c / 2)) + c // 2 * 3
		else:
			c = x - y
			s = s + int(math.ceil(c / 2)) * 3 + c // 2
		print(s)


for i in range(int(input())):
	(x, y) = list(map(int, input().split()))
	X = abs(x)
	Y = abs(y)
	if X == Y or Y == X + 1:
		steps = X + Y
	elif X > Y:
		d = X - Y
		steps = 2 * Y + 2 * d + d % 2
	else:
		d = Y - (X + 1)
		steps = 2 * X + 1 + 2 * d + d % 2
	print(steps)


t = int(input())
for t0 in range(t):
	(x, y) = [abs(int(i)) for i in input().strip().split(' ')]
	m = min(x, y)
	tot = m * 2
	if x > y:
		d = x - y
		if d % 2 == 0:
			tot += d * 2
		else:
			tot += d * 2 + 1
	elif x < y:
		d = y - x
		if d % 2 == 0:
			tot += d * 2
		else:
			tot += d * 2 - 1
	print(tot)


def next(x, y):
	if x == 0 and y == 0:
		return 0
	elif x == 0 and y != 0:
		if y % 2 == 1:
			return 2 * y - 1
		else:
			return 2 * y
	elif x != 0 and y == 0:
		if x % 2 == 1:
			return 2 * x + 1
		else:
			return 2 * x
t = int(input())
for i in range(t):
	A = list(map(eval, input().split()))
	x = abs(A[0])
	y = abs(A[1])
	z = min(x, y)
	print(2 * z + next(x - z, y - z))


t = int(input())
for i in range(0, t):
	(x, y) = input().strip().split()
	x = abs(int(x))
	y = abs(int(y))
	if x < y:
		s = 2 * x
		d = (y - x) // 2
		e = (y - x) % 2
		print(s + 4 * d + e)
	elif x == y:
		print(2 * x)
	else:
		s = 2 * y
		d = (x - y) // 2
		e = (x - y) % 2
		print(s + 4 * d + 3 * e)