Problem: Robert Angier is divising a new magic trick to beat his rival Alfred Borden. He is going to perform a card trick. In this trick, cards are numbered from 1 to N. This trick involves the following process. Angier will invite a person from audience to shuffle the cards in random order.     

Following is defined as a single move:     
He will take a card from top of the deck, place card facing downwards on a table. Again he will take a card from top of the deck and place it below the deck. For example if current deck is suppose ABCDE, after one move A will be on the table (facing downwards) and the state of deck will be CDEB.     

Angier after taking the random shuffled deck, will perform the above move many times until only one card is left. After only one card is left, he will put that card also on the table. Now Angier will turn all the cards present on the table so that number on them is visible. To astonishment of everyone the cards are numbered in sequence from 1 to N.       

To perform this trick, Angier will put his assistant among the audience, who will arrange the cards into the required order. For example, let us suppose the deck was of size 6 and all cards were numbered from 1 to 6. His assistant will arrange the deck into 1,4,2,6,3,5. The moves when applied onto such a deck will result in 1,2,3,4,5,6. You need to tell to his assistant at what position should the card numbered K should be placed.       

For example in the above case where N=6, if K=6, the answer will be 4 since card numbered 6 is placed at 4th position.        

Input Format 
First line contains T, the number of testcases. Each testcase consists of two space seperated integers denoting N and K.       

Output Format 
For each testcase, print in one line, the required answer.           

Constraints 
1 ≤ T ≤ 100  
1 ≤ N ≤ 10^9 
1 ≤ K ≤ N

SAMPLE INPUT
1
6 6

SAMPLE OUTPUT
4

Solution (in PYTHON):

Solution:
import math

n = int(raw_input())

for ii in range(n):
	N,K = map(int,raw_input().split())
	
	if N % 2 == 0 and K <= N/2:
		print K*2-1
	elif N % 2 == 1 and K <= N/2+1:	
		print K*2-1
	elif N-K == 0:
		pp = int(math.log(N)/math.log(2) - 1e-5)
		print int(2*(N-math.pow(2,pp)))
	else:
		ff = (3+2*(N-K-1))
		pp = int( math.log((N - 1e-5)/ff)/math.log(2))+1
		print int(2*N-ff*math.pow(2,pp))