Chef has $K$ chocolates and he wants to distribute them to $N$ people (numbered $1$ through $N$). These people are standing in a line in such a way that for each $i$ ($1 \le i \le N-1$), person $i$ and person $i+1$ are adjacent.
First, consider some way to distribute chocolates such that for each valid $i$, the number of chocolates the $i$-th person would receive from Chef is $A_i$ and the sum $S_1 = \sum_{i=1}^{N-1} \left|A_i - A_{i+1}\right|$ is minimum possible. Of course, each person must receive a non-negative integer number of chocolates.
Then, Chef wants to create a new sequence $B_1, B_2, \ldots, B_N$ by rearranging (permuting) the elements of the sequence $A_1, A_2, \ldots, A_N$. For each valid $i$, the number of chocolates the $i$-th person actually receives from Chef is $B_i$. Chef wants to distribute the chocolates (choose $B_1, B_2, \ldots, B_N$ by permuting the sequence $A$ and give $B_i$ chocolates to the $i$-th person for each valid $i$) in such a way that $S_2 = \sum_{i=1}^{N-1} \left|B_i – B_{i+1}\right|$ is maximum possible. You need to find the maximum value of $S_2$.
It is guaranteed that $S_2$ does not depend on the exact choice of the sequence $A_1, A_2, \ldots, A_N$, as long as it is a sequence that minimises $S_1$.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains a single integer $K$.

-----Output-----
For each test case, print a single line containing one integer — the maximum value of the sum $S_2$.

-----Constraints-----
- $1 \le T \le 10$
- $2 \le N \le 10^5$
- $2 \le K \le 10^{100,000}$

-----Subtasks-----
Subtask #1 (30 points): $2 \le N, K \le 1,000$
Subtask #2 (70 points): original constraints

-----Example Input-----
1
3
2

-----Example Output-----
2

-----Explanation-----
Example case 1: To minimise $S_1$, Chef could give $1$ chocolate to person $1$ and $1$ chocolate to person $2$, so $S_1 = |1-1| + |1-0| = 1$.
To maximise $S_2$, Chef can give $1$ chocolate to person $1$ and $1$ chocolate to person $3$, since the sequence $B = (1, 0, 1)$ is a permutation of $A = (1, 1, 0)$. Then, $S_2 = |1-0| + |0-1| = 2$.

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	k = int(input())
	num = int(k / n)
	x = max(n * (1 + num) - k, 0)
	diff = abs(x - (n - x))
	if diff == 0:
		number = 2 * x - 1
	else:
		number = min(x, n - x) * 2
	print(number)


def solve():
	n = int(input())
	k = int(input())
	cnt1 = k % n
	cnt0 = n - cnt1
	if cnt1 < cnt0:
		print(2 * cnt1)
	elif cnt1 > cnt0:
		print(2 * cnt0)
	else:
		print(2 * cnt1 - 1)

def __starting_point():
	t = int(input())
	for _1 in range(t):
		solve()
__starting_point()


def Test():
	n = int(input())
	k = int(input())
	low = min(k % n, n - k % n)
	print(min(n - 1, low * 2))
T = int(input())
for t in range(T):
	Test()


testcase = int(input())
for ij in range(testcase):
	n = int(input())
	k = int(input())
	list1 = []
	list2 = []
	list3 = [0]
	if n % 2 != 0:
		f = n // 2
		for i in range(f):
			list1.append(2 * (i + 1))
		for i in range(len(list1)):
			list2.append(list1[i])
		for i in range(len(list1)):
			list2.append(list1[f - 1 - i])
	if n % 2 == 0:
		s = (n - 1) // 2
		for i in range(s):
			list1.append(2 * (i + 1))
		for i in range(s):
			list2.append(list1[i])
		list2.append(list1[s - 1] + 1)
		for i in range(s):
			list2.append(list1[s - 1 - i])
	list2.append(0)
	p = k % n
	print(list2[p - 1])


t = int(input())
for test in range(t):
	n = int(input())
	k = int(input())
	one = k % n
	zero = n - one
	if zero < one:
		(one, zero) = (zero, one)
	if zero >= one + 1:
		print(one * 2)
	elif zero == one:
		print(2 * zero - 1)
	else:
		print((zero - 1) * 2)


t = int(input())
while t != 0:
	n = int(input())
	k = int(input())
	if k % n == 0:
		print(0)
	else:
		p = k % n
		if n % 2 == 0 and p == n / 2:
			print(2 * p - 1)
		elif p > n // 2:
			print(2 * (n - p))
		else:
			print(2 * p)
	t -= 1


for i in range(int(input())):
	N = int(input())
	K = int(input())
	min1 = min(K % N, N - K % N)
	print(min(N - 1, min1 * 2))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	N = int(input())
	K = int(input())
	min1 = min(K % N, N - K % N)
	print(min(N - 1, min1 * 2))


import sys, math
input = sys.stdin.readline

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

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return list(map(int, input().split()))
t = inp()
for _ in range(t):
	n = inp()
	k = inp()
	x = k % n
	y = n - x
	if x == 0:
		print(0)
	elif x == y:
		ans = 2 * x - 1
		print(ans)
	else:
		ans = min(x, y) * 2
		print(ans)


for t in range(int(input())):
	n = int(input())
	k = int(input())
	low = min(k % n, n - k % n)
	print(min(n - 1, low * 2))


for _ in range(int(input())):
	n = int(input())
	k = int(input())
	if k % n == 0:
		print(0)
	elif n % 2 == 0:
		x = n // 2
		if k % x == 0:
			print(n - 1)
		else:
			y = k % n
			if y > x:
				y = n - y
			print(2 * y)
	else:
		x = n // 2
		y = x + 1
		if (k - x) % n == 0 or (k - y) % n == 0:
			print(n - 1)
		else:
			z = k % n
			if z > x:
				z = n - z
			print(2 * z)


t = int(input())
for _ in range(t):
	n = int(input())
	k = int(input())
	x1 = k // n
	y1 = x1 + 1
	y2 = k % n
	x2 = n - y2
	ans = 0
	i = 0
	j = 0
	l = []
	while i < x2 and j < y2:
		if y2 >= x2:
			l += [1, 0]
		else:
			l += [0, 1]
		i += 1
		j += 1
	while i < x2:
		l.append(0)
		i += 1
	while j < y2:
		l.append(1)
		j += 1
	ans = 0
	for i in range(1, len(l)):
		ans += abs(l[i] - l[i - 1])
	print(ans)


T = int(input())
while T:
	n = int(input())
	k = int(input())
	if k % n == 0:
		print(0)
	elif k % n == n - k % n:
		print(2 * (k % n) - 1)
	elif k % n > n - k % n:
		print(2 * (n - k % n))
	elif k % n < n - k % n:
		print(2 * (k % n))
	T -= 1


t = int(input())
for _ in range(t):
	N = int(input())
	K = int(input())
	num_1 = K % N
	num_0 = N - num_1
	ans = 0
	if num_1 > num_0:
		ans = num_0 * 2
	elif num_0 > num_1:
		ans = num_1 * 2
	else:
		ans = num_1 * 2 - 1
	print(ans)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	k = int(input())
	num_1 = k % n
	num_0 = n - num_1
	ans = 0
	if num_0 > num_1:
		ans = num_1 * 2
	elif num_0 < num_1:
		ans = num_0 * 2
	else:
		ans = num_1 * 2 - 1
	print(ans)


T = int(input())
for i in range(T):
	n = int(input())
	k = int(input())
	r = k % n
	if r == n - r:
		print(n - 1)
	else:
		print(2 * min(r, n - r))


def __starting_point():
	T = int(input())
	while T > 0:
		T = T - 1
		n = int(input())
		num = int(input())
		num = num % n
		num2 = n - num
		if num2 == num:
			print(num * 2 - 1)
		else:
			print(min(num, num2) * 2)
__starting_point()


t = int(input())
while t > 0:
	n = int(input())
	k = int(input())
	high = k % n
	low = n - high
	if high == low:
		ans = 2 * high - 1
		print(ans)
	else:
		mn = min(high, low)
		ans = 2 * mn
		print(ans)
	t = t - 1


t = int(input())
for i in range(t):
	n = input()
	k = input()
	k = int(k)
	n = int(n)
	w = k % n
	if w == n / 2:
		print(n - 1)
	else:
		y = n - w
		y = min(y, w)
		print(2 * y)


t = int(input())
while t != 0:
	n = int(input())
	k = int(input())
	i = k % n
	if i * 2 == n:
		print(n - 1)
	else:
		print(min(i, n - i) * 2)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	k = int(input())
	if k % n == 0:
		print(0)
	else:
		low = min(k % n, n - k % n)
		print(min(n - 1, low * 2))


t = int(input())
for i in range(t):
	n = int(input())
	k = int(input())
	a = k % n
	if n % 2 == 0:
		if a > n // 2:
			a = n - a
			print(2 * a)
		elif a < n // 2:
			print(2 * a)
		else:
			print(2 * a - 1)
	elif a > n // 2:
		a = n - a
		print(2 * a)
	else:
		print(2 * a)