Chef prepared two dishes yesterday. Chef had assigned the \textbf{tastiness} T_{1} and T_{2} to the first and to the second dish respectively. The tastiness of the dishes can be any \textbf{integer} between 0 and N (both inclusive).

Unfortunately, Chef has forgotten the values of T_{1} and T_{2} that he had assigned to the dishes. However, he remembers the sum of the tastiness of both the dishes - denoted by S.

Chef wonders, what can be the maximum possible absolute difference between the tastiness of the two dishes. Can you help the Chef in finding the maximum absolute difference?

It is guaranteed that at least one pair \{T_{1}, T_{2}\} exist such that T_{1} + T_{2} = S, 0 ≤ T_{1}, T_{2} ≤ N.

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

- The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows.
- The first and only line of each test case contains two space-separated integers N, S, denoting the maximum tastiness and the sum of tastiness of the two dishes, respectively.

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

For each testcase, output a single line containing the maximum absolute difference between the tastiness of the two dishes.

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

$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ S ≤ 2 \cdot 10^{5}$

----- Sample Input 1 ------ 
3
3 1
4 4
2 3
----- Sample Output 1 ------ 
1
4
1
----- explanation 1 ------ 
Test Case $1$: The possible pairs of $\{T_{1}, T_{2}\}$ are $\{0, 1\}$ and $\{1, 0\}$. Difference in both the cases is $1$, hence the maximum possible absolute difference is $1$.

Test Case $2$: The possible pairs of $\{T_{1}, T_{2}\}$ are $\{0, 4\}$, $\{1, 3\}$, $\{2, 2\}$, $\{3, 1\}$ and $\{4, 0\}$. The maximum possible absolute difference is $4$.

Test Case $3$: The possible pairs of $\{T_{1}, T_{2}\}$ are $\{1, 2\}$ and $\{2, 1\}$. Difference in both the cases is $1$, and hence the maximum possible absolute difference is $1$.

Difficulty: EASY
Tags: Constructive algorithms

Mathematics

Ad-hoc

Starter code:

Solutions:
def main():
	T = int(input())
	while T > 0:
		diff()
		T -= 1

def diff():
	(N, S) = map(int, input().split())
	t1 = min(N, S)
	t2 = S - t1
	max_diff = t1 - t2
	print(max_diff)
main()


t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	if n < s:
		print(2 * n - s)
	else:
		print(s)


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	if s >= n:
		q = s - n
		print(abs(n - q))
	else:
		print(s)


for tc in range(int(input())):
	(n, s) = map(int, input().split())
	if s > n:
		print(abs(2 * n - s))
	else:
		print(s)


for _ in range(int(input())):
	(n, s) = map(int, input().strip().split())
	if s <= n:
		print(s)
	else:
		print(2 * n - s)


for _ in range(int(input())):
	NS = list(map(int, input().split()))
	N = NS[0]
	S = NS[1]
	if N > S:
		print(S)
	elif N == S:
		print(N)
	else:
		print(N - (S - N))


t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	t1 = min(n, s)
	t2 = s - t1
	print(t1 - t2)


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	if s == n:
		res = s
	elif n > s:
		res = s
	elif n < s:
		res = n - (s - n)
	print(res)


for _ in range(int(input())):
	d = 1
	(n, s) = map(int, input().split())
	(a, b) = (0, s)
	while b > n:
		a += 1
		b -= 1
	print(abs(a - b))


t = int(input())
for i in range(0, t):
	(n, s) = map(int, input().split())
	a = abs(n - abs(n - s))
	print(a)


t = int(input())
while t > 0:
	(n, s) = list(map(int, input().split()))
	a = 0
	b = s
	while b > n:
		a += 1
		b -= 1
	print(abs(a - b))
	t -= 1


for i in range(int(input())):
	(n, s) = map(int, input().split())
	print(abs(n - abs(n - s)))


t = int(input())
for tc in range(t):
	(n, s) = map(int, input().split())
	a = 0
	b = s
	while not b <= n:
		a += 1
		b -= 1
	print(abs(a - b))


def absDiff(n, s):
	if s <= n:
		return s
	else:
		return n - (s - n)
t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	print(absDiff(n, s))


t = int(input())
while t:
	(n, s) = map(int, input().split())
	t1 = min(n, s)
	t2 = s - t1
	print(t1 - t2)
	t -= 1


t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	sum = s - n
	if n < s and s != 0:
		print(n - sum)
	elif n > s and n != 0:
		print(s)
	else:
		print(n)


c = int(input())
te = []
for nn in range(0, c):
	k = str(input())
	hn = []
	for uu in k.split(' '):
		hn.append(int(uu))
	te.append(hn)
for bb in te:
	if bb[1] == 1:
		print(1)
	elif bb[0] == bb[1]:
		print(bb[1])
	elif bb[1] < bb[0]:
		print(bb[1])
	else:
		nm = bb[1] - bb[0]
		trc = bb[0] - nm
		print(trc)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a >= b:
		print(b)
	else:
		print(a - (b - a))


t = int(input())
for i in range(t):
	(n, s) = map(int, input(' ').split(' ', 2))
	maxi = 0
	mini = 0
	if n >= s:
		maxi = s
		mini = 0
	else:
		maxi = n
		mini = s - n
	print(maxi - mini)


t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	n = l[0]
	s = l[1]
	if n == s:
		print(n)
	elif n > s:
		print(s)
	else:
		d = s - n
		print(abs(d - n))


from sys import stdin, stdout
input = stdin.readline
r_int = lambda : int(input())
m_int = lambda : map(int, input().split())
l_int = lambda : list(map(int, input()).split())

def solve():
	(n, s) = m_int()
	if n >= s:
		print(s)
	else:
		print(2 * n - s)

def main():
	n_cases = r_int()
	for _ in range(n_cases):
		solve()
main()


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	s = abs(a - b)
	print(a - s)


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	t1 = min(n, s)
	t2 = s - t1
	print(t1 - t2)


for t in range(int(input())):
	(n, s) = map(int, input().split())
	x = int((n + s) / 2)
	y = max(n, s)
	diff = y - x
	if n >= s:
		print(s)
	else:
		print(n - (s - n))


t = int(input())
while t:
	t -= 1
	(n, s) = map(int, input().split())
	k = s - n
	if n < s and s != 0:
		print(n - k)
	elif n > s and n != 0:
		print(s)
	else:
		print(n)


t = int(input())
while t:
	i = input()
	(n, s) = map(int, i.split())
	if s > n:
		k = s - n
		print(n - k)
	else:
		print(s)
	t = t - 1


t = int(input())
for i in range(t):
	(N, S) = map(int, input().split())
	if S <= N:
		print(S)
	else:
		print(N - (S - N))


def func(Num, Sum):
	if Sum <= Num:
		return print(Sum)
	else:
		temp = 0
		for i in range(1, Num + 1):
			if Sum - i <= Num:
				temp = Sum - 2 * i
				break
		return print(temp)
t = int(input())
for i in range(t):
	(Num, Sum) = map(int, input().split())
	func(Num, Sum)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a >= b:
		print(b)
	else:
		print(a - b + a)


l = []
for _ in range(int(input())):
	(max_taste, add) = map(int, input().split())
	n1 = min(max_taste, add)
	n2 = add - n1
	diff = n1 - n2
	l.append(diff)
for i in l:
	print(i)


T = int(input())
for _ in range(T):
	[n, s] = map(int, input().split())
	t1 = s
	t2 = 0
	if t1 > n:
		t1 = n
		t2 = s - t1
	print(abs(t1 - t2))


T = int(input())
x = range(T)
for i in x:
	(n, s) = map(int, input().split())
	if n >= s:
		print(s)
	else:
		print(2 * n - s)


t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	diff = 0
	if s <= n:
		diff = s
	else:
		diff = n - s + n
	print(diff)


z = int(input())
for i in range(z):
	(n, s) = map(int, input().split())
	if s <= n:
		print(s)
	else:
		print(n + n - s)


t = int(input())
while t > 0:
	(n, s) = list(map(int, input().split()))
	x = 0
	y = s
	while not y <= n:
		x += 1
		y -= 1
	print(abs(x - y))
	t -= 1


for _ in range(int(input())):
	(p, s) = map(int, input().split())
	if p > s:
		print(s)
	else:
		a = p
		b = s - p
		print(abs(b - a))


for i in range(int(input())):
	(N, S) = map(int, input().split())
	if S == 1:
		print('1')
	elif S == N:
		print(N)
	elif N < S:
		print(N - (S - N))
	elif N > S:
		print(S)


t = int(input())
for i in range(1, t + 1):
	(n, s) = map(int, input().split())
	if n > s:
		print(s)
	else:
		a = n
		b = s - n
		print(abs(b - a))


t = int(input())
for l in range(t):
	x = input().split()
	n = int(x[0])
	s = int(x[1])
	if s > n:
		print(2 * n - s)
	else:
		print(s)


for i in range(int(input())):
	(x, B) = map(int, input().split())
	if x >= B:
		print(B)
	else:
		print(x - (B - x))


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	if n >= s:
		print(s)
	else:
		y = s - n
		print(n - y)


for _ in range(int(input())):
	(N, S) = map(int, input().split())
	if N == S:
		print(N)
	elif N < S:
		print(N - abs(S - N))
	elif N > S:
		print(S)


t = int(input())
for ni in range(t):
	(n, s) = map(int, input().split())
	if n >= s:
		print(s)
	else:
		a = n
		b = s - n
		print(a - b)


def main():
	test = int(input())
	while test > 0:
		diff()
		test -= 1

def diff():
	(N1, S1) = map(int, input().split())
	ta1 = min(N1, S1)
	ta2 = S1 - ta1
	max_diff = ta1 - ta2
	print(max_diff)
main()