There are two problems in a contest.
Problem A is worth 500 points at the start of the contest.
Problem B is worth 1000 points at the start of the contest.

Once the contest starts, after each minute:
Maximum points of Problem A reduce by 2 points .
Maximum points of Problem B reduce by 4 points.

It is known that Chef requires X minutes to solve Problem A correctly and Y minutes to solve Problem B correctly. 

Find the maximum number of points Chef can score if he optimally decides the order of attempting both the problems.

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

- First line will contain T, number of test cases. Then the test cases follow.
- Each test case contains of a single line of input, two integers X and Y - the time required to solve problems A and B in minutes respectively.

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

For each test case, output in a single line, the maximum number of points Chef can score if he optimally decides the order of attempting both the problems.

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

$1 ≤ T ≤ 1000$
$1 ≤ X, Y ≤ 100$

----- Sample Input 1 ------ 
4
10 20
8 40
15 15
20 10

----- Sample Output 1 ------ 
1360
1292
1380
1400

----- explanation 1 ------ 
Test Case $1$: If Chef attempts in the order $A \rightarrow B$ then he submits Problem A after $10$ minutes and Problem B after $30$ minutes.  
Thus, he gets $500 - 10 \cdot 2 = 480$ points for problem A and $1000 - 30 \cdot 4 = 880$ points for problem B. Thus, total $480+880=1360$ points for both the problems.

If Chef attempts in the order $B \rightarrow A$ then he submits Problem B after $20$ minutes and Problem A after $30$ minutes.  
Thus, he gets $1000 - 20 \cdot 4 = 920$ points for Problem B and $500 - 30 \cdot 2 = 440$ points for Problem A. Thus total $920+440=1360$ points for both the problems.

So, in both cases Chef gets $1360$ points in total.

Test Case $2$: If Chef attempts in the order $A \rightarrow B$ then he submits Problem A after $8$ minutes and Problem B after $48$ minutes.  
Thus, he gets $500 - 8 \cdot 2 = 484$ points for problem A and $1000 - 48 \cdot 4 = 808$ points for problem B. Thus, total $484+808=1292$ points for both the problems.

If Chef attempts in the order $B \rightarrow A$ then he submits Problem B after $40$ minutes and Problem A after $48$ minutes.  
Thus, he gets $1000 - 40 \cdot 4 = 840$ points for Problem B and $500 - 48 \cdot 2 = 404$ points for Problem A. Thus total $840+404=1244$ points for both the problems.

So, Chef will attempt in the order $A \rightarrow B$ and thus obtain $1292$ points.

Test Case $3$: If Chef attempts in the order $A \rightarrow B$ then he submits Problem A after $15$ minutes and Problem B after $30$ minutes.  
Thus, he gets $500 - 15 \cdot 2 = 470$ points for problem A and $1000 - 30 \cdot 4 = 880$ points for problem B. Thus, total $470+880=1350$ points for both the problems.

If Chef attempts in the order $B \rightarrow A$ then he submits Problem B after $15$ minutes and Problem A after $30$ minutes.  
Thus, he gets $1000 - 15 \cdot 4 = 940$ points for Problem B and $500 - 30 \cdot 2 = 440$ points for Problem A. Thus total $940+440=1380$ points for both the problems.

So, Chef will attempt in the order $B \rightarrow A$ and thus obtain $1380$ points.

Test Case $4$: If Chef attempts in the order $A \rightarrow B$ then he submits Problem A after $20$ minutes and Problem B after $30$ minutes.  
Thus, he gets $500 - 20 \cdot 2 = 460$ points for problem A and $1000 - 30 \cdot 4 = 880$ points for problem B. Thus, total $460+880=1340$ points for both the problems.

If Chef attempts in the order $B \rightarrow A$ then he submits Problem B after $10$ minutes and Problem A after $30$ minutes.  
Thus, he gets $1000 - 10 \cdot 4 = 960$ points for Problem B and $500 - 30 \cdot 2 = 440$ points for Problem A. Thus total $960+440=1400$ points for both the problems.

So, Chef will attempt in the order $B \rightarrow A$ and thus obtain $1400$ points.

Source: codechef | Difficulty: EASY
Tags: Mathematics


Solutions:
t = int(input())
for i in range(t):
	(X, Y) = map(int, input().split())
	FirstA = 500 - 2 * X + (1000 - 4 * (X + Y))
	FirstB = 1000 - 4 * Y + (500 - 2 * (X + Y))
	if FirstB > FirstA:
		print(FirstB)
	else:
		print(FirstA)


t = int(input())
for i in range(0, t):
	(x, y) = map(int, input().split())
	a = 500 - x * 2 + (1000 - (x + y) * 4)
	b = 1000 - y * 4 + (500 - (x + y) * 2)
	print(max(a, b))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	d = x + y
	A = 500
	B = 1000
	pba = A - x * 2 + (B - d * 4)
	pbb = B - y * 4 + (A - d * 2)
	print(max(pba, pbb))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	ab = 500 - 2 * x + (1000 - 4 * (x + y))
	ba = 1000 - 4 * y + (500 - 2 * (x + y))
	if ab >= ba:
		print(ab)
	else:
		print(ba)


for i in range(int(input())):
	(a, b) = map(int, input().split(' '))
	print(max(500 - a * 2 + (1000 - (a + b) * 4), 500 - b * 4 + (1000 - (a + b) * 2)))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	z = x + y
	f = 500 - x * 2
	g = 1000 - z * 4
	h = f + g
	p = 1000 - y * 4
	q = 500 - z * 2
	r = p + q
	if h > r:
		print(h)
	else:
		print(r)


x = int(input(''))
for l in range(x):
	(a, b) = map(int, input().split())
	if 500 - a * 2 + 1000 - (b + a) * 4 > 500 - (a + b) * 2 + 1000 - b * 4:
		print(500 - a * 2 + 1000 - (b + a) * 4)
	else:
		print(500 - (a + b) * 2 + 1000 - b * 4)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	a = 500 - 2 * x
	b = 1000 - 4 * (x + y)
	c = a + b
	k = 1000 - 4 * y
	l = 500 - 2 * (x + y)
	m = k + l
	if c >= m:
		print(c)
	else:
		print(m)


T = int(input())
for i in range(T):
	(X, Y) = map(int, input().split())
	b = Y * 4
	a = (X + Y) * 2
	t1 = 1500 - (a + b)
	c = X * 2
	d = (X + Y) * 4
	t2 = 1500 - (c + d)
	if t1 >= t2:
		print(t1)
	else:
		print(t2)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	r = 500 - a * 2 + (1000 - (a + b) * 4)
	c = 1000 - b * 4 + (500 - (a + b) * 2)
	print(max(r, c))


x = int(input())
for i in range(x):
	(a, b) = map(int, input().split())
	c = 500 - a * 2 + (1000 - (a + b) * 4)
	d = 1000 - b * 4 + (500 - (a + b) * 2)
	if c < d:
		print(d)
	else:
		print(c)


t = int(input())
a = 500
b = 1000
for i in range(t):
	(x, y) = map(int, input().split())
	am1 = x * 2
	bm2 = (x + y) * 4
	bm1 = y * 4
	am2 = (x + y) * 2
	tt = a + b - (am1 + bm2)
	ttt = a + b - (am2 + bm1)
	print(max(tt, ttt))


T = int(input())
for i in range(T):
	(X, Y) = map(int, input().split())
	print(max(500 - X * 2 + (1000 - (X + Y) * 4), 1000 - Y * 4 + (500 - (X + Y) * 2)))


t = int(input())
for i in range(t):
	(X, Y) = map(int, input().split())
	P = 500 - X * 2 + (1000 - (X + Y) * 4)
	Q = 1000 - Y * 4 + (500 - (X + Y) * 2)
	print(max(P, Q))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	m = a * 2
	n = (a + b) * 4
	l = 500 - m + (1000 - n)
	k = b * 4
	j = (b + a) * 2
	o = 1000 - k + (500 - j)
	if l > o:
		print(l)
	else:
		print(o)


for i in range(int(input())):
	x = [int(x) for x in input().split()]
	print(max([500 - x[0] * 2 + (1000 - (x[0] + x[1]) * 4), 1000 - x[1] * 4 + (500 - (x[0] + x[1]) * 2)]))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	z = x + y
	a = 500 - x * 2
	b = 1000 - z * 4
	c = a + b
	d = 1000 - y * 4
	e = 500 - z * 2
	f = d + e
	print(max(c, f))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	x = 500 - a * 2
	y = 1000 - (a + b) * 4
	r1 = x + y
	c = 1000 - b * 4
	d = 500 - (a + b) * 2
	r2 = c + d
	if r1 >= r2:
		print(r1)
	else:
		print(r2)


for i in range(int(input())):
	(x, y) = map(int, input().split(' '))
	first = 500 - x * 2 + (1000 - (x + y) * 4)
	second = 1000 - y * 4 + (500 - (x + y) * 2)
	print(max(first, second))


T = int(input())
for z in range(T):
	(a, b) = map(int, input().split())
	c = 500 - a * 2 + 1000 - (a + b) * 4
	d = 1000 - b * 4 + 500 - (a + b) * 2
	if c >= d:
		print(c)
	else:
		print(d)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	a1 = 500 - x * 2
	b1 = 1000 - (x + y) * 4
	sum1 = a1 + b1
	a2 = 1000 - y * 4
	b2 = 500 - (x + y) * 2
	sum2 = a2 + b2
	print(max(sum1, sum2))


def points(x, y):
	a_first = 1500 - x * 2 - (y + x) * 4
	b_first = 1500 - y * 4 - (y + x) * 2
	return max(a_first, b_first)
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split(' '))
	print(points(x, y))


for i in range(int(input())):
	(x, y) = map(int, input().split())
	i = 500 - x * 2 + 1000 - (y + x) * 4
	j = 500 - (x + y) * 2 + 1000 - y * 4
	if i > j:
		print(i)
	else:
		print(j)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	m = x * 2 + (x + y) * 4
	n = y * 4 + (x + y) * 2
	if m >= n:
		a = 1500 - n
	else:
		a = 1500 - m
	print(a)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	c = 500 - x * 2
	d = 1000 - (x + y) * 4
	c2 = 1000 - y * 4
	d2 = 500 - (x + y) * 2
	e = c + d
	f = c2 + d2
	if e > f:
		print(e)
	else:
		print(f)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	q = 500 - x * 2
	w = 1000 - (x + y) * 4
	e = q + w
	r = 1000 - y * 4
	y = 500 - (x + y) * 2
	u = r + y
	print(max(e, u))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	a = 500 - x * 2
	b = 1000 - (x + y) * 4
	p = 1000 - y * 4
	q = 500 - (x + y) * 2
	nithin = a + b
	chow = p + q
	if chow >= nithin:
		print(chow)
	else:
		print(nithin)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	m = 500 - x * 2
	n = 1000 - x * 4 - y * 4
	k = m + n
	o = 1000 - y * 4
	p = 500 - y * 2 - x * 2
	l = o + p
	if k > l:
		print(k)
	else:
		print(l)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	a = 500 - x * 2 + 1000 - (x + y) * 4
	b = 1000 - y * 4 + 500 - (y + x) * 2
	if a == b:
		print(a)
	elif a > b:
		print(a)
	else:
		print(b)


n = int(input())
for i in range(0, n):
	(a, b) = map(int, input().split())
	k1 = 500 - a * 2
	k2 = 1000 - (a + b) * 4
	p1 = 1000 - b * 4
	p2 = 500 - (a + b) * 2
	x = k1 + k2
	y = p1 + p2
	if x >= y:
		print(x)
	else:
		print(y)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	f1 = 500 - a * 2
	f2 = 1000 - (a + b) * 4
	s1 = 1000 - b * 4
	s2 = 500 - (a + b) * 2
	if f1 + f2 >= s1 + s2:
		print(f1 + f2)
	else:
		print(s1 + s2)


x = 500
y = 1000
for i in range(int(input())):
	(a, s) = map(int, input().split())
	t = x - 2 * a + y - 4 * (s + a)
	q = x - 2 * (s + a) + y - 4 * s
	print(max(t, q))


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	p = int(500 - x * 2 + (1000 - (x + y) * 4))
	q = int(1000 - y * 4 + (500 - (x + y) * 2))
	print(max(p, q))


def prob(x, y):
	p = 500 - x * 2 + (1000 - (x + y) * 4)
	q = 500 - y * 4 + (1000 - (x + y) * 2)
	return max(p, q)
t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	v = prob(x, y)
	print(v)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	c = 500 - x * 2 + (1000 - (x + y) * 4)
	d = 1000 - y * 4 + (500 - (x + y) * 2)
	print(max(c, d))


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	x = a + b
	z = 500 - a * 2
	q = 1000 - x * 4
	s = z + q
	t = 1000 - b * 4
	p = 500 - x * 2
	g = t + p
	if s > g:
		print(s)
	else:
		print(g)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	x = 500 - a * 2
	y = 1000 - (a + b) * 4
	z = x + y
	w = 500 - b * 4
	q = 1000 - (a + b) * 2
	e = w + q
	print(max(z, e))


t = int(input())
for i in range(t):
	(X, Y) = map(int, input().split())
	(A, B) = (2, 4)
	T = 500 - X * A
	U = 1000 - (X + Y) * B
	P = 1000 - Y * B
	O = 500 - (X + Y) * A
	print(max(T + U, P + O))


for t in range(int(input())):
	(x, y) = map(int, input().split())
	z = x + y
	a = 500 - x * 2
	b = 1000 - z * 4
	c = 1000 - y * 4
	d = 500 - z * 2
	print(c + d) if a + b < c + d else print(a + b)


t = int(input())
for _ in range(t):
	(x, y) = list(map(int, input().split()))
	f1 = 500 - x * 2 + 1000 - 4 * (x + y)
	f2 = 1000 - 4 * y + 500 - 2 * (x + y)
	print(max(f1, f2))


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	a1 = 500 - 2 * x
	b1 = 1000 - 4 * (x + y)
	a2 = 500 - 2 * (x + y)
	b2 = 1000 - 4 * y
	print(max(a1 + b1, a2 + b2))


n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	lst = []
	a = 500
	b = 1000
	f1 = a - 2 * x
	s1 = b - 4 * (y + x)
	a_b = f1 + s1
	f2 = a - 2 * (x + y)
	s2 = b - 4 * y
	b_a = f2 + s2
	lst.append(a_b)
	lst.append(b_a)
	print(max(lst))


t = int(input())
for i in range(0, t):
	(x, y) = [int(i) for i in input().split()]
	o1 = 1000 - y * 4 + (500 - (x + y) * 2)
	o2 = 1000 - (x + y) * 4 + (500 - x * 2)
	if o1 > o2:
		print(o1)
	else:
		print(o2)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	s1 = 500 - 2 * x + 1000 - 4 * (x + y)
	s2 = 1000 - 4 * y + 500 - 2 * (x + y)
	print(max(s1, s2))


T = int(input())
if T <= 1000 and T >= 1:
	for i in range(0, T):
		(X, Y) = map(int, input().split())
		R = 500 - X * 2 + (1000 - (Y + X) * 4)
		G = 1000 - Y * 4 + (500 - (X + Y) * 2)
		if R >= G:
			print(R)
		elif G >= R:
			print(G)
		else:
			print(R)


T = int(input())
for i in range(T):
	(x, y) = map(int, input().split())
	a_then_b = 500 - 2 * x + (1000 - 4 * (x + y))
	b_then_a = 1000 - 4 * y + (500 - 2 * (x + y))
	if a_then_b > b_then_a:
		print(a_then_b)
	else:
		print(b_then_a)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	c = 1500 - a * 2 - (a + b) * 4
	d = 1500 - b * 4 - (a + b) * 2
	if d >= c:
		print(d)
	else:
		print(c)


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	(ab1, ab2) = (a, a + b)
	(res1, res2) = (500 - ab1 * 2, 1000 - ab2 * 4)
	sum1 = res1 + res2
	(ba1, ba2) = (b, a + b)
	(res3, res4) = (1000 - ba1 * 4, 500 - ba2 * 2)
	sum2 = res3 + res4
	print(max(sum1, sum2))


a = int(input())
for i in range(a):
	b = [int(x) for x in input().split()]
	c = 500 - b[0] * 2 + (1000 - (b[1] + b[0]) * 4)
	d = 1000 - b[1] * 4 + (500 - (b[0] + b[1]) * 2)
	print(max(c, d))


T = int(input())
for i in range(T):
	(X, Y) = map(int, input().split())
	a = 500 - X * 2 + (1000 - (Y + X) * 4)
	b = 1000 - Y * 4 + (500 - (Y + X) * 2)
	if a < b:
		print(int(b))
	else:
		print(int(a))