There are N cards on a table, out of which X cards are face-up and the remaining are face-down.

In one operation, we can do the following:
Select any one card and flip it (i.e. if it was initially face-up, after the operation, it will be face-down and vice versa) 

What is the minimum number of operations we must perform so that all the cards face in the same direction (i.e. either all are face-up or all are face-down)?

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

- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two space-separated integers N and X — the total number of cards and the number of cards which are initially face-up.

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

For each test case, output the minimum number of cards you must flip so that all the cards face in the same direction.

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

$1 ≤ T ≤ 5000$
$2 ≤ N ≤ 100$
$0 ≤ X ≤ N$

----- Sample Input 1 ------ 
4
5 0
4 2
3 3
10 2

----- Sample Output 1 ------ 
0
2
0
2

----- explanation 1 ------ 
Test Case 1: All the cards are already facing down. Therefore we do not need to perform any operations.

Test Case 2: $2$ cards are facing up and $2$ cards are facing down. Therefore we can flip the $2$ cards which are initially facing down.

Test Case 3: All the cards are already facing up. Therefore we do not need to perform any operations.

Test Case 4: $2$ cards are facing up and $8$ cards are facing down. Therefore we can flip the $2$ cards which are initially facing up.

Difficulty: EASY
Tags: Mathematics

Starter code:

Solutions:
n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	if y == 0 or x - y == 0:
		print('0')
	else:
		a = x - y
		if a > y:
			print(y)
		else:
			print(a)


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	if b != a:
		print(min(b, a - b))
	else:
		print(0)


t = int(input())
for tc in range(t):
	(n, x) = map(int, input().split())
	print(min(n - x, x))


t = int(input())
for t in range(t):
	(n, x) = map(int, input().split())
	if n != x:
		print(min(x, n - x))
	else:
		print(0)


T = int(input())
for i in range(T):
	(N, X) = map(int, input().split())
	print(min(X, N - X))


T = int(input())
for i in range(T):
	(N, X) = map(int, input().split())
	if N // 2 < X:
		print(N - X)
	else:
		print(X)


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


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	if x == y:
		print(0)
	else:
		print(min(x - y, y))


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


T = int(input())
for i in range(T):
	(N, X) = map(int, input().split())
	if N == X or X == 0:
		print(0)
	elif X > int(N / 2):
		print(N - X)
	else:
		print(X)


for _ in range(int(input())):
	(N, X) = map(int, input().split())
	Y = N - X
	print(min(X, Y))


for i in range(int(input())):
	(a, b) = map(int, input().split())
	if b <= a / 2:
		print(b)
	elif b > a / 2:
		a -= b
		print(a)
	elif a == b:
		print('0')


def Flip(n, x):
	if x == 0 or x == n:
		print(0)
	elif n - x < x:
		print(n - x)
	else:
		print(x)
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	Flip(n, x)


n = int(input())
for i in range(n):
	x = input()
	x = list(map(int, x.split()))
	m = x[0] - x[1]
	if m > x[1]:
		print(x[1])
	else:
		print(m)


a = int(input())
for i in range(a):
	(b, c) = map(int, input().split())
	if c == 0 or b - c == 0:
		print('0')
	else:
		d = b - c
		if d > c:
			print(c)
		else:
			print(d)


t = int(input())
for i in range(t):
	(y, x) = map(int, input().split(' '))
	z = y - x
	if x < z:
		print(x)
	elif x == 0:
		print(0)
	else:
		print(z)


def min_flips(n, x):
	min_flips_all_up = min(x, n - x)
	min_flips_all_down = min(n - x, x)
	return min(min_flips_all_up, min_flips_all_down)
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	print(min_flips(n, x))


T = int(input())
for i in range(T):
	(N, X) = map(int, input().split())
	if X != N:
		print(min(X, N - X))
	else:
		print(0)


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


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


t = int(input())
while t != 0:
	s = input()
	l = [int(i) for i in s.split()]
	n = l[0]
	up = l[1]
	if up == 0 or up == n:
		print(0)
	else:
		ans = min(n - up, up)
		print(ans)
	t = t - 1


for i in range(int(input())):
	(x, y) = map(int, input().split())
	print(min(y, x - y))


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	m = n - x
	if n == x:
		print(0)
	elif m < x:
		print(m)
	else:
		print(x)


t = int(input())
for i in range(t):
	(n, x) = list(map(int, input().split(' ')))
	if n == x:
		print(0)
	elif n > x and x == 0:
		print(0)
	else:
		y = min(x, n - x)
		print(y)


T = int(input())

def minface(A, B):
	return min(A, B)
for i in range(T):
	(N, X) = map(int, input().split())
	print(minface(X, N - X))


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


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


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	if x == n or x == 0:
		print(0)
	else:
		print(min(n - x, x))


t = int(input())
for i in range(0, t):
	(n, x) = input().split()
	if int(x) == 0 or int(n) == int(x):
		print(0)
	elif int(x) >= int(n) - int(x):
		print(int(n) - int(x))
	else:
		print(int(x))


T = int(input())
for i in range(T):
	(n, x) = map(int, input().split())
	if x == 0 or x == n:
		print(0)
	else:
		print(min(x, n - x))


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	c = a - b
	d = min(b, c)
	print(d)


t = int(input())
for x in range(t):
	(n, x) = list(map(int, input().split()))
	if n == x or x == 0:
		print(0)
	elif n - x > x:
		print(x)
	else:
		print(n - x)


T = int(input())
i = 1
while i <= T:
	(N, X) = map(int, input().split())
	if X == 0 or N == X:
		print('0')
	elif N != X:
		print(min(X, N - X))
	i += 1


def min_operations(n, x):
	return min(x, n - x)
t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	print(min_operations(n, x))


n = int(input())
for i in range(n):
	(x, y) = [int(x) for x in input().split()]
	if y == 0:
		print('0')
	elif x == y:
		print('0')
	else:
		a = x - y
		b = min(a, y)
		print(b)


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


n = int(input())
while n:
	ip1 = input().split()
	l1 = [int(i) for i in ip1]
	print(min(l1[0], l1[1], l1[0] - l1[1]))
	n -= 1


for _ in range(int(input())):
	(N, X) = map(int, input().split())
	if N - X < X:
		print(N - X)
	else:
		print(X)


t = int(input())
for i in range(t):
	(a, b) = list(map(int, input().split()))
	if a == b or b == 0:
		print('0')
	elif a > b:
		sub = a - b
		print(min(sub, b))


m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if a == b or b == 0:
		print('0')
	elif a > b:
		print(min(b, a - b))


for i in range(int(input())):
	(n, x) = map(int, input().split())
	a = n / 2
	if n == x:
		print(0)
	elif a <= x:
		print(n - x)
	else:
		print(x)


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


m = int(input())
for i in range(m):
	(x, y) = map(int, input().split())
	s = 0
	if x != y:
		print(min(y, x - y))
	else:
		print(0)


for _ in range(int(input())):
	(N, X) = map(int, input().split())
	if N == X or X == 0:
		print('0')
	elif N - X < X:
		print(N - X)
	else:
		print(X)