Akash and Akhil are playing a game. They have $N$ balls numbered from $0$ to $N-1$. Akhil asks Akash to reverse the position of the balls, i.e., to change the order from say, 0,1,2,3 to 3,2,1,0. He further asks Akash to reverse the position of the balls $N$ times, each time starting from one position further to the right, till he reaches the last ball. So, Akash has to reverse the positions of the ball starting from ${0}^{t h}$ position, then from $1^{st}$ position, then from $2^{nd}$ position and so on. At the end of the game, Akhil will ask Akash the final position of any ball numbered $\mbox{K}$. Akash will win the game, if he can answer. Help Akash.

Input Format 

The first line contains an integer $\mathbf{T}$, i.e., the number of the test cases. 

The next $\mathbf{T}$ lines will contain two integers $N$ and $\mbox{K}$. 

Output Format 

Print the final index of ball $\mbox{K}$ in the array.  

Constraints 

$1\leq T\leq50$ 

$1\leq N\leq10^5$ 

$0\leq K\lt N$

Sample Input  

2
3 1
5 2

Sample Output  

2
4

Explanation 

For first test case, The rotation will be like this: 

0 1 2 -> 2 1 0 -> 2 0 1 -> 2 0 1

So, Index of 1 will be 2.

Difficulty: EASY
Tags: Mathematics

Starter code:

Solutions:
T = int(input())
for i in range(T):
	(N, K) = [int(x) for x in input().split()]
	for i in range(0, N):
		if K >= i:
			K = i + N - 1 - K
	print(K)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if k < n // 2:
		print(2 * k + 1)
	else:
		print(2 * (n - k - 1))


import fractions, functools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, K) = list(map(int, sys.stdin.readline().split()))
	if K < N - K - 1:
		print(2 * K + 1)
	else:
		print(2 * (N - K - 1))


def main():
	t = int(input())
	while t != 0:
		t = t - 1
		(n, k) = [int(i) for i in input().split(' ')]
		final = []
		l = 0
		for i in range(n):
			if i % 2 == 0:
				final.append(n - l - 1)
			else:
				final.append(l)
				l = l + 1
		for i in range(n):
			if final[i] == k:
				print(i)
main()


def calc(a, b):
	split = a // 2 - 1
	if b <= split:
		return 2 * b + 1
	else:
		return 2 * (a - 1 - b)
tcs = int(input())
for tc in range(tcs):
	(a, b) = map(int, input().split(' '))
	print(calc(a, b))


t = int(input())
for _ in range(t):
	ID = input().split()
	n = int(ID[0])
	k = int(ID[1])
	if k < n // 2:
		print(k + k + 1)
	else:
		print(2 * (n - 1 - k))


t = int(input())
for i in range(t):
	x = input().split(' ')
	n = int(x[0])
	k = int(x[1])
	if 2 * k + 1 < n:
		ans = 2 * k + 1
	else:
		ans = (-2 * k - 2) % n
	print(ans)


def solve_line(line_no, line):
	(n, k) = map(int, line.split())
	return 2 * k + 1 if k < (n - 1) / 2 else (n - k - 1) * 2

def solve(inputs):
	next(inputs)
	for (line_no, line) in enumerate(inputs, start=1):
		yield solve_line(line_no, line)

def interact(fun):

	def inputs():
		try:
			while True:
				yield input()
		except EOFError:
			pass
	outputs = fun(inputs())
	if outputs is not None:
		for line in outputs:
			print('' if line is None else line)
interact(solve)


T = int(input())
for i in range(T):
	(n, k) = [int(val) for val in input().split()]
	halfwayIndex = int(n / 2)
	if k < halfwayIndex:
		print(k * 2 + 1)
	else:
		distanceFromLast = n - 1 - k
		print(distanceFromLast * 2)


T = int(input())

def merge(array1, array2):
	array = []
	for i in range(len(array2)):
		array.append(array1[i])
		array.append(array2[i])
	if len(array1) > len(array2):
		array.append(array1[len(array1) - 1])
	return array
for _ in range(T):
	(N, K) = input().split()
	N = int(N)
	K = int(K)
	array = list(range(N))
	array.reverse()
	split = len(array) // 2
	evens = array[:split + 1]
	odds = array[split + 1:]
	odds.reverse()
	array = merge(evens, odds)
	print(array.index(K))


t = int(input())
for x in range(0, t):
	(n, k) = [int(i) for i in input().strip().split()]
	balls = []
	for y in range(0, int(n // 2)):
		balls.append(n - 1 - y)
		balls.append(0 + y)
	if n % 2 == 1:
		balls.append(n // 2)
	print(balls.index(k))


for _ in range(int(input())):
	n = input().split()
	k = int(n[1])
	n = int(n[0])
	pos = k
	for i in range(n):
		if i <= pos:
			pos = n - 1 - (pos - i)
	print(pos)


T = int(input())
for _ in range(T):
	(N, K) = map(int, input().split())
	if K < N // 2:
		print(2 * K + 1)
	elif K == N // 2:
		print(2 * K)
	else:
		print((N - K - 1) * 2)


from sys import stdin
T = int(stdin.readline())
for _ in range(T):
	(N, K) = [int(s) for s in stdin.readline().split()]
	if K < N // 2:
		print(K + K + 1)
	else:
		print((N - K - 1) * 2)


def findball(nums, find):
	for k in range(len(nums)):
		i = int(k / 2)
		res = 0
		if k % 2 == 0:
			res = nums[-(i + 1)]
		else:
			res = nums[i]
		if res == find:
			return k
tests = int(input())
for i in range(tests):
	(N, K) = map(int, input().split())
	nums = [i for i in range(N)]
	print(findball(nums, K))


for i in range(int(input())):
	(n, k) = (int(j) for j in input().split())
	print(k * 2 + 1 if k * 2 + 1 < n else (n - k - 1) * 2)


T = int(input())
for i in range(T):
	NK = input().split(' ')
	N = int(NK[0])
	K = int(NK[1])
	l = [i for i in range(N)]
	l1 = []
	n = N - 1
	ind = 0
	for i in range(N):
		if l[n] == K:
			print(ind)
			break
		n = n - 1
		ind += 1
		if l[i] == K:
			print(ind)
			break
		ind += 1


num = int(input())
for i in range(num):
	str = input().split()
	str = [int(x) for x in str]
	k = 0
	l = str[0] - 1
	mynewlist = []
	for j in range(str[0]):
		if len(mynewlist) != str[0]:
			mynewlist.append(l)
			l = l - 1
		if len(mynewlist) != str[0]:
			mynewlist.append(k)
			k = k + 1
	print(mynewlist.index(str[1]))


def game(N, K):
	li = list(range(N))
	for x in range(N):
		li[x:N] = reversed(li[x:N])
	return li.index(K)

def game2(N, K):
	if K >= N // 2:
		return (N - K - 1) * 2
	else:
		return (K + 1) * 2 - 1
T = int(input())
for t in range(T):
	r = input()
	r = r.split()
	N = int(r[0])
	K = int(r[1])
	print(game2(N, K))


t = int(input())
i = 0
while i < t:
	inp = input()
	n = int(inp.split(' ')[0])
	k = int(inp.split(' ')[1])
	j = 0
	a = []
	while j < n:
		a.append(j)
		j = j + 1
	j = 0
	pres = a.index(k)
	while j < n and pres >= j:
		pres = n - 1 + j - pres
		j = j + 1
	print(pres)
	i = i + 1


T = int(input())
for t in range(T):
	(N, K) = map(int, input().split())
	for i in range(N):
		if K < i:
			break
		K = i + N - 1 - K
	print(K)


T = int(input())
for t in range(T):
	(n, k) = (int(s) for s in input().split())
	i = 0
	j = n - 1
	current = j
	for a in range(n):
		aIsEven = a % 2 == 0
		if aIsEven:
			current = j
			j -= 1
		else:
			current = i
			i += 1
		if current == k:
			print(a)
			break


for (N, K) in ((int(i) for i in input().split(' ')) for _ in range(int(input()))):
	lastI = int((N - 1) // 2)
	add = N - N // 2 * 2
	if K == lastI:
		print(N - 1)
	elif K < lastI:
		print(N - 1 - abs(lastI - K) * 2 + add)
	else:
		print(N - 1 - abs(lastI - K) * 2 + (1 - add))


T = int(input())
while T:
	tmp = input().split()
	K = int(tmp[1])
	N = int(tmp[0])
	if K * 2 + 1 < N:
		print(K * 2 + 1)
	else:
		print(2 * (N - 1 - K))
	T -= 1


t = input()
l = []
for i in range(0, int(t)):
	m = input()
	l.append((int(m.split()[0]), int(m.split()[1])))
for i in l:
	n = i[0]
	k = i[1]
	if k >= int(n / 2):
		a = 2 * (n - k - 1)
	else:
		a = 2 * (k + 1) - 1
	print(a)


T = int(input())
for x in range(T):
	a = input().split(' ')
	N = int(a[0])
	K = int(a[1])
	i = 0
	j = N - 1
	flip = True
	count = 0
	for x in range(N):
		if i == K:
			print(count + 1)
			break
		if j == K:
			print(count + 1)
			break
		if flip:
			j -= 1
			count += 1
			flip = False
		else:
			i += 1
			count += 1
			flip = True


for ti in range(int(input())):
	(n, k) = map(int, input().split())
	(n, l) = (n - 1, 0)
	while l <= k:
		(k, l) = (n + l - k, l + 1)
	print(k)


def ans(n, k):
	if k < n // 2:
		return k * 2 + 1
	else:
		return (N - 1 - k) * 2
T = int(input())
for _ in range(T):
	(N, K) = map(int, input().split())
	print(ans(N, K))


def reverse(n, k):
	if n % 2 == 0:
		if k >= n / 2:
			print((n - (k + 1)) * 2)
		else:
			print(2 * k + 1)
	elif k >= int(n / 2):
		print((n - (k + 1)) * 2)
	else:
		print(2 * k + 1)
testCase = int(input())
for i in range(0, testCase):
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	reverse(n, k)


def g(n, i):
	return (i > (n - 1) // 2) * 2 * (n - 1 - i) + (i < (n - 1) // 2) * (1 + 2 * i) + (i == (n - 1) // 2) * (n - 1)
i = int(input())
while i > 0:
	i -= 1
	i2 = input().split(' ')
	n = int(i2[0])
	k = int(i2[1])
	print(g(n, k))


for i in range(int(input())):
	nums = input()
	entered = nums.split()
	nums = int(entered[0])
	pos = int(entered[1])
	finished = []
	lower = nums // 2
	lower_add = 0
	upper_add = nums - 1
	for i in range(nums):
		if i % 2:
			if lower_add < lower:
				finished.append(lower_add)
				lower_add += 1
		elif upper_add > lower - 1:
			finished.append(upper_add)
			upper_add -= 1
	print(finished.index(pos))


for i in range(int(input())):
	(n, k) = map(int, input().split())
	if n % 2 == 0:
		if k <= (n - 1) // 2:
			print(2 * k + 1)
		else:
			print(2 * (n - k - 1))
	elif k < (n - 1) // 2:
		print(2 * k + 1)
	else:
		print(2 * (n - k - 1))


import sys

def makearray(n):
	mod = n % 2
	value = n - 1
	array = []
	for i in range(n // 2):
		a = value - i
		array.append(a)
		array.append(value - a)
	if mod != 0:
		array.append(n // 2)
	return array

def reverse_game(n, k):
	array = makearray(n)
	return array.index(k)
line = sys.stdin.readline()
t = int(line)
for i in range(t):
	line = sys.stdin.readline()
	(n, k) = map(int, line.split())
	print(reverse_game(n, k))


import math
for t in range(int(input())):
	(N, num) = [int(x) for x in input().split()]
	if num >= math.floor(N / 2):
		print((N - num - 1) * 2)
	else:
		print(num * 2 + 1)


for t in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	if b < a // 2:
		print(2 * b + 1)
	else:
		print((a - 1 - b) * 2)


def main():
	t = eval(input())
	for i in range(t):
		[n, k] = input().split()
		[n, k] = [int(n), int(k)]
		if k == (n - 1) // 2:
			print(n - 1)
		elif k < (n - 1) / 2:
			print(2 * k + 1)
		else:
			print(2 * (n - (k + 1)))
main()


def f(n, k):
	if k < n // 2:
		return 2 * k + 1
	else:
		return 2 * (n - 1 - k)
for _ in range(int(input())):
	print(f(*[int(i) for i in input().split()]))


for i in range(int(input())):
	(n, k) = map(int, input().split())
	if k < n // 2:
		print(2 * k + 1)
	else:
		print(2 * (n - k - 1))


T = int(input())
for _ in range(T):
	(N, K) = map(int, input().split())
	if K < N // 2:
		print(K * 2 + 1)
	else:
		print((N - K - 1) * 2)


tests = int(input())
for i in range(0, tests):
	cur = list((int(x) for x in str(input()).split(' ')))
	dbeg = cur[1]
	dend = cur[0] - cur[1] - 1
	if dbeg < dend:
		print(1 + 2 * dbeg)
	else:
		print(2 * dend)


for case in range(int(input())):
	(n, k) = map(int, input().split())
	if k >= n // 2:
		print(2 * (n - k - 1))
	else:
		print(2 * k + 1)


for t in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	for i in range(n):
		if k >= i:
			k = n - 1 - k + i
		else:
			break
	print(k)


n = int(input())
for i in range(n):
	line = input().split(' ')
	balls = int(line[0])
	pos = int(line[1])
	if pos > int(balls / 2):
		print((balls - 1 - pos) * 2)
	elif pos < int(balls / 2):
		print(1 + pos * 2)
	else:
		print(balls - 1)


def main():
	number_of_test_cases = int(input())
	for test_case in range(number_of_test_cases):
		(n, k) = list(map(int, input().strip().split(' ')))
		print(min(2 * k + 1, (n - k - 1) * 2))
main()


from math import ceil
from math import floor
T = int(input())
for t in range(T):
	d = input().split()
	(N, K) = (int(d[0]), int(d[1]))
	balls = [b for b in range(N)]
	a = ceil(N / 2)
	b = floor(N / 2)
	new_balls = []
	new_balls_1 = [x for x in range(N - 1, a - 2, -1)]
	new_balls_2 = [x for x in range(b + 1)]
	for i in range(b):
		new_balls.append(new_balls_1[i])
		new_balls.append(new_balls_2[i])
	if a > b:
		new_balls.append(new_balls_1[-1])
	print(new_balls.index(K))


a = int(input())
for _ in range(0, a):
	b = list(map(int, input().split(' ')))
	for i in range(0, b[0]):
		if i == 0:
			c = [b[0] - 1]
		elif i == 1:
			c.append(0)
		elif i % 2 == 0:
			c.append(c[i - 2] - 1)
		else:
			c.append(c[i - 2] + 1)
		if c[i] == b[1]:
			print(i)
			break


import cmath
for test in range(int(input())):
	(N, K) = tuple(map(int, input().split()))
	tab = [N - 1]
	(i, j) = (0, N - 2)
	while i <= N // 2:
		tab += [i]
		if i == K:
			print(len(tab) - 1)
			break
		i += 1
		tab += [j]
		if j == K:
			i += 1
			print(len(tab) - 1)
			break
		j -= 1


T = int(input())
while T != 0:
	s = input()
	smas = s.split(' ')
	N = int(smas[0])
	K = int(smas[1])
	if K < N // 2:
		pos = 2 * K + 1
	else:
		pos = 2 * (N - K - 1)
	print(pos)
	T -= 1


def main():
	for case in range(int(input())):
		(n, k) = [int(x) for x in str(input()).split()]
		print(check(n, k))

def check(n, k):
	if k < int(0.5 * n):
		return 1 + 2 * k
	else:
		return 2 * (n - k - 1)
main()


import math

def solve(N, K):
	if K < math.floor(N / 2):
		pos = 2 * (K + 1)
	if K >= math.floor(N / 2):
		pos = 2 * (N - K) - 1
	return pos - 1
T = int(input())
pos = 0
while T > 0:
	(hn, hk) = input().split()
	N = int(hn)
	K = int(hk)
	print(solve(N, K))
	T = T - 1


cases = int(input())
for _ in range(cases):
	(balls, q) = [int(i) for i in input().split()]
	if balls // 2 == q:
		if balls % 2 == 0:
			print(balls - 2)
		else:
			print(balls - 1)
	elif balls // 2 > q:
		print(2 * q + 1)
	else:
		print((balls - 1 - q) * 2)


import math
T = int(input())
for i in range(0, T):
	inp = input()
	(N, K) = inp.split()
	N = int(N)
	K = int(K)
	if N % 2 == 0:
		if K + 1 <= N / 2:
			print(math.floor(2 * (K + 1) - 1))
		else:
			K = K + 1 - N / 2
			print(math.floor(N - 2 * K))
	elif K + 1 <= N / 2:
		print(math.floor(2 * (K + 1) - 1))
	else:
		K = N - (K + 1)
		K = 2 * K + 1
		K = K - 1
		print(math.floor(K))


for _ in range(int(input())):
	(n, k) = map(int, input().strip().split())
	first = 0
	last = n - 1
	if last - k <= k - first:
		print(2 * (last - k))
	else:
		print(2 * (k - first) + 1)


for _ in range(int(input().strip())):
	(n, k) = map(int, input().strip().split(' '))
	for i in range(n):
		if 0 == i % 2 and k == n - 1 - i // 2 or (1 == i % 2 and k == i // 2):
			print(i)
			break


for _ in range(int(input())):
	(ballCount, pos) = (int(x) for x in input().split(' '))
	if pos < ballCount // 2:
		print(2 * pos + 1)
	else:
		print(2 * (ballCount - pos - 1))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, K) = list(map(int, sys.stdin.readline().split()))
	print(min(1 + 2 * K, 2 * (N - 1 - K)))


for x in range(int(input())):
	(n, k) = map(int, input().split())
	if n % 2 == 0:
		mid = n // 2
		sub = 0
	else:
		mid = n // 2 + 1
		sub = 1
	if k < mid:
		p = k * 2 + 1
	else:
		p = (n - 1 - k) * 2
	if k == mid - 1:
		p -= sub
	print(p)


numcases = int(input())
for i in range(numcases):
	(n, k) = [int(x) for x in input().split()]
	if k < (n - 1) // 2:
		print(1 + 2 * k)
	else:
		print((n - 1 - k) * 2)


T = int(input())
for i in range(0, T):
	inp = []
	final_inp = []
	lists = [int(i) for i in input().split()]
	N = lists[0]
	for x in range(0, N):
		inp.append(x)
	K = lists[1]
	index = int(N / 2)
	list1 = inp[0:index]
	list2 = inp[index:N]
	list1.reverse()
	while len(list1) != 0:
		final_inp.append(list2.pop())
		final_inp.append(list1.pop())
	final_inp = final_inp + list2
	print(final_inp.index(K))


numtest = int(input())
for i in range(numtest):
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	l = [x for x in range(n)]
	l2 = []
	for i in range(int(n / 2)):
		l2.append(l[n - i - 1])
		l2.append(l[i])
	if n % 2 == 1:
		l2.append(l[int(n / 2)])
	print(l2.index(k))


t = int(input())
res = []
for i in range(t):
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	m = list(range(n))
	l = []
	for j in range(n // 2):
		l.append(m[n - 1 - j])
		l.append(m[j])
	if n % 2 != 0:
		l.append(m[n // 2])
	res.append(l.index(k))
for i in res:
	print(i)


a = int(input())
for i in range(a):
	c = input().split(' ')
	(l, n) = (int(c[0]), int(c[1]))
	if n < (l - 1) / 2:
		print(2 * n + 1)
	else:
		print(2 * (l - 1 - n))


for _ in range(int(input())):
	(N, K) = map(int, input().split())
	string = [int(i) for i in range(N)]
	mid = int(len(string) / 2)
	str1 = string[:mid]
	str2 = string[mid:][::-1]
	if K in str1:
		print(2 * str1.index(K) + 1)
	else:
		print(2 * str2.index(K))


T = int(input())
for t in range(0, T):
	line = [int(z) for z in input().split()]
	start = 0
	end = line[0] - 1
	i = 0
	while True:
		if end == line[1]:
			print(i)
			break
		end -= 1
		i += 1
		if start == line[1]:
			print(i)
			break
		i += 1
		start += 1


T = int(input())
for i in range(T):
	(n, k) = [int(ns) for ns in str(input()).split()]
	if k < int(n / 2):
		print(2 * k + 1)
	else:
		print(2 * (n - k - 1))


def final_position(N, K):
	mid = N // 2 - 1
	if K > mid:
		K = N - K - 1
		return K * 2
	return K * 2 + 1
T = int(input())
for i in range(T):
	(N, K) = [int(x) for x in input().split()]
	print(final_position(N, K))


T = int(input())
for _ in range(T):
	(N, K) = [int(i) for i in input().split()]
	print(1 + 2 * K if K < N // 2 else 2 * (N - 1 - K))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = [_ for _ in range(n)]
	i = 0
	j = n - 1
	s = []
	while i < j:
		s += [a[j], a[i]]
		i += 1
		j -= 1
	if i == j:
		s += [a[i]]
	print(s.index(k))


def findReverseIndex(N, K):
	if K < int(N / 2):
		return 1 + 2 * K
	else:
		return 2 * (N - 1 - K)
t = int(input())
for case in range(t):
	inputs = input().split()
	N = int(inputs[0])
	K = int(inputs[1])
	print(findReverseIndex(N, K))


t = int(input())
for _ in range(t):
	(n, k) = [int(v) for v in input().split()]
	if k < int(n / 2):
		print(k * 2 + 1)
	else:
		print(2 * (n - k - 1))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split(' '))
	(j, k) = (x - 1, 0)
	ans = []
	for l in range(x):
		if l & 1:
			ans.append(k)
			k += 1
		else:
			ans.append(j)
			j -= 1
	print(ans.index(y))


t = int(input())
while t:
	t = t - 1
	arr = [int(i) for i in input().split()]
	(a, b) = (arr[0], arr[1])
	a = a - 1
	if b >= int(a / 2):
		temp = 0
		for i in range(a - b):
			temp = temp + 2
		print(temp)
	else:
		temp = 1
		for i in range(b):
			temp = temp + 2
		print(temp)


T = int(input())
for i in range(0, T):
	N_K = str(input())
	N_K = N_K.split(' ')
	N = int(N_K[0])
	K = int(N_K[1])
	N_DIV_2 = (N - 1) // 2
	if K < N_DIV_2:
		print(K * 2 + 1)
	else:
		print(N - 1 - K + (N - 1 - K))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, K) = list(map(int, sys.stdin.readline().split()))
	print(min(1 + 2 * K, 2 * (N - 1 - K)))


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	if k < n // 2:
		offset = 1 + k * 2
	else:
		offset = (n - k - 1) * 2
	print(offset)


def guess_index(N, ball_num):
	if ball_num < N / 2.0 - N % 2:
		return 2 * ball_num + 1
	else:
		return 2 * (N - 1 - ball_num)
T = int(input())
for i in range(T):
	(N, K) = map(int, input().strip().split())
	print(guess_index(N, K))


for t in range(int(input())):
	(n, k) = [int(a) for a in input().split()]
	if k < (n - 1) / 2:
		print(1 + 2 * k)
	else:
		print(2 * (n - k - 1))


n = int(input())
for i in range(n):
	(length, final_test) = input().split()
	(length, final_test) = (int(length), int(final_test))
	if final_test < (length - 1) // 2:
		print(2 * final_test + 1)
	else:
		print(2 * (length - final_test) - 2)


t = int(input())
while t:
	t = t - 1
	s = input()
	s = list(map(int, s.split()))
	(n, k) = (s[0], s[1])
	if n % 2 == 0:
		if n / 2 > k:
			print(int(2 * (k + 1) - 1))
		else:
			print(int(n - 1 - k) * 2)
	elif n // 2 > k:
		print(int(2 * (k + 1) - 1))
	elif n / 2 < k:
		print(int(n - 1 - k) * 2)
	else:
		print(n - 1)


for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	if k < n // 2:
		print(k * 2 + 1)
	else:
		print((n - 1 - k) * 2)


def reverse(A, N, K):
	A1 = [0] * N
	i = 0
	j = N - 1
	k = 0
	while i <= j:
		if i == j:
			A1[k] = A[i]
		else:
			A1[k] = A[j]
			A1[k + 1] = A[i]
			k += 2
		i += 1
		j -= 1
	return A1.index(K)
cases = int(input())
results = []
for case in range(cases):
	line = input().split()
	N = int(line[0])
	K = int(line[1])
	A = list(range(N))
	results.append(reverse(A, N, K))
for result in results:
	print(result)


t = int(input())
while t > 0:
	l = []
	fl = []
	l1 = []
	l2 = []
	a = input()
	a = list(map(int, a.split()))
	n = a[0]
	fk = a[1]
	for i in range(0, n):
		l.append(i)
	for k in range(0, n):
		if k % 2 != 0:
			fl.append(int(k / 2))
		else:
			fl.append(n - int((k + 2) / 2))
	print(fl.index(fk))
	t -= 1


a = int(input())
answer = 0
for i in range(a):
	(b, c) = input().split(' ')
	(b, c) = (int(b) - 1, int(c))
	if b % 2:
		if c < b / 2:
			answer = c * 2 + 1
		else:
			answer = 2 * (b - c)
	elif c >= b / 2:
		answer = 2 * (b - c)
	else:
		answer = c * 2 + 1
	print(answer)


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, K) = list(map(int, sys.stdin.readline().split()))
	print(min(1 + 2 * K, 2 * (N - 1 - K)))


t = int(input())
for ii in range(t):
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	for i in range(n):
		if i % 2 == 0:
			x = n - i // 2 - 1
		else:
			x = i // 2
		if x == k:
			print(i)
			break


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, K) = list(map(int, sys.stdin.readline().split()))
	print(min(1 + 2 * K, 2 * (N - 1 - K)))


t = int(input())
for i in range(t):
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	div = n // 2
	if n % 2 == 0:
		if k < div:
			res = (k + 1) * 2
		else:
			res = 1 + (n - 1 - k) * 2
	elif k - 1 == div + 1:
		res = n
	elif k < div:
		res = (k + 1) * 2
	else:
		res = 1 + (n - 1 - k) * 2
	print(res - 1)


for t in range(int(input())):
	(N, K) = list(map(int, input().split()))
	N = range(N)
	l = 0
	r = len(N) - 1
	res = []
	for i in range(len(N) // 2):
		res.append(N[r])
		res.append(N[l])
		l += 1
		r -= 1
	if l == r:
		res.append(N[r])
	for i in range(len(res)):
		if res[i] == K:
			print(i)
			break


def solve(n, k):
	if k < n // 2:
		return k * 2 + 1
	elif k == n // 2:
		return n - 1
	else:
		return n - 1 - (k * 2 + 1) % n
for _ in range(int(input())):
	(n, k) = input().split()
	print(solve(int(n), int(k)))


for i in range(int(input())):
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	if n == 1:
		print('0')
		continue
	if n & 1:
		mid = int((n - 2) / 2)
	else:
		mid = int((n - 1) / 2)
	if k <= mid:
		print(2 * k + 1)
	else:
		print((n - 1 - k) * 2)


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	k += 1
	if k <= n // 2:
		print(2 * (k - 1) + 1)
	else:
		print(2 * (n - k))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, K) = list(map(int, sys.stdin.readline().split()))
	print(min(1 + 2 * K, 2 * (N - 1 - K)))


n = int(input())
for i in range(n):
	a = input()
	x = []
	for i in a.split():
		x.append(i)
	N = int(x[0])
	T = int(x[1])
	if T < N // 2:
		print(2 * T + 1)
	else:
		print(2 * (N - 1) - T * 2)


T = int(input())
for _ in range(T):
	(N, K) = input().split()
	(N, K) = (int(N), int(K))
	print(2 * K + 1) if K < (N - 1) // 2 else print(2 * (N - K - 1))


from fileinput import input

def find():
	lines = input()
	noc = [int(x) for x in lines[0].strip().split()][0]
	for i in range(1, noc + 1):
		(n, k) = [int(x) for x in lines[i].strip().split()]
		if k < n // 2:
			print(2 * k + 1, end='\n')
		else:
			print((n - 1 - k) * 2, end='\n')
find()


num_cases = int(input())
for _ in range(num_cases):
	(N, K) = [int(x) for x in input().split()]
	if K >= (N - 1) // 2:
		print((N - K - 1) * 2)
	else:
		print(K * 2 + 1)


def reverse(n, k):
	end_count = n - 1
	start_count = 0
	out = []
	for i in range(n // 2):
		out.append(end_count)
		out.append(start_count)
		end_count -= 1
		start_count += 1
	if n % 2 != 0:
		out.append(end_count)
	return out.index(k)
test_cases = int(input())
for i in range(test_cases):
	(n, ball_number) = [int(x) for x in input().split()]
	index = reverse(n, ball_number)
	print(index)


t = input()
for x in range(0, int(t)):
	a = input()
	(n, k) = a.split()
	n = int(n)
	k = int(k)
	b = []
	m = n - 1
	v = 0
	for i in range(0, n):
		if i % 2 == 0:
			b.append(m)
			m = m - 1
		else:
			b.append(v)
			v = v + 1
	print(b.index(k))


import math
t = int(input())
for test in range(t):
	(n, k) = map(int, input().split())
	l = list(range(n))
	record = {}
	for i in range(len(l)):
		if i < len(l) // 2:
			record[i] = 2 * i + 1
		else:
			record[i] = (len(l) - 1 - i) * 2
	print(record[k])