atcoder#AGC061A. [AGC061A] Long Shuffle

[AGC061A] Long Shuffle

Score : 500500 points

Problem Statement

There is an array A1,,ANA_1, \ldots, A_N, and initially Ai=iA_i = i for all ii. Define the following routine shuffle(L,R)\mathrm{shuffle}(L, R):

  • If R=L+1R = L + 1, swap values of ALA_L and ARA_R and terminate.
  • Otherwise, run shuffle(L,R1)\mathrm{shuffle}(L, R - 1) followed by shuffle(L+1,R)\mathrm{shuffle}(L + 1, R).

Suppose we run shuffle(1,N)\mathrm{shuffle}(1, N). Print the value of AKA_K after the routine finishes.

For each input file, solve TT test cases.

Constraints

  • 1T10001 \leq T \leq 1000
  • 2N10182 \leq N \leq 10^{18}
  • 1KN1 \leq K \leq N

Input

The input is given from Standard Input in the following format:

TT

case1case_1

case2case_2

\vdots

caseTcase_T

Each case is in the following format:

NN KK

Output

Print TT lines. The ii-th line should contain the answer for the ii-th case.

7
2 1
2 2
5 1
5 2
5 3
5 4
5 5
2
1
2
4
1
5
3

For N=2N=2, we do the following and get A=(2,1)A=(2,1).

  • Run shuffle(1,2)\mathrm{shuffle}(1, 2), and swap A1A_1 and A2A_2.

For N=5N=5, we do the following and get A=(2,4,1,5,3)A=(2,4,1,5,3).

  • Run shuffle(1,5)\mathrm{shuffle}(1, 5):- Run shuffle(1,4)\mathrm{shuffle}(1, 4):- Run shuffle(1,3)\mathrm{shuffle}(1, 3):- \vdots
    • Run shuffle(2,4)\mathrm{shuffle}(2, 4):- \vdots
    • Run shuffle(2,5)\mathrm{shuffle}(2, 5):- Run shuffle(2,4)\mathrm{shuffle}(2, 4):- \vdots
    • Run shuffle(3,5)\mathrm{shuffle}(3, 5):- \vdots
  • Run shuffle(1,4)\mathrm{shuffle}(1, 4):
  • Run shuffle(1,3)\mathrm{shuffle}(1, 3):
  • \vdots
  • Run shuffle(2,4)\mathrm{shuffle}(2, 4):
  • \vdots
  • Run shuffle(2,5)\mathrm{shuffle}(2, 5):
  • Run shuffle(2,4)\mathrm{shuffle}(2, 4):
  • \vdots
  • Run shuffle(3,5)\mathrm{shuffle}(3, 5):
  • \vdots