CF BUDDY
← Problems·

1926E · Vlad and an Odd Ordering

1500 · binary search, bitmasks, data structures

Problem: Vladislav has nn cards numbered 1,2,,n1, 2, \dots, n. He wants to lay them down in a row as follows:

  • First, he lays down all the odd-numbered cards from smallest to largest.
  • Next, he lays down all cards that are twice an odd number from smallest to largest (i.e. 22 multiplied by an odd number).
  • Next, he lays down all cards that are 33 times an odd number from smallest to largest (i.e. 33 multiplied by an odd number).
  • Next, he lays down all cards that are 44 times an odd number from smallest to largest (i.e. 44 multiplied by an odd number).
  • And so on, until all cards are laid down.

Input Format: The first line contains an integer tt (1t51041 \leq t \leq 5 \cdot 10^4) — the number of test cases.

The only line of each test case contains two integers nn and kk (1kn1091 \leq k \leq n \leq 10^9) — the number of cards Vlad has, and the position of the card you need to output.

Output Format: For each test case, output a single integer — the kk-th card Vladislav lays down.

Note: In the first seven test cases, n=7n=7. Vladislav lays down the cards as follows:

  • First — all the odd-numbered cards in the order 11, 33, 55, 77.
  • Next — all cards that are twice an odd number in the order 22, 66.
  • Next, there are no remaining cards that are 33 times an odd number. (Vladislav has only one of each card.)
  • Next — all cards that are 44 times an odd number, and there is only one such card: 44.
  • There are no more cards left, so Vladislav stops.

Sample Cases

Case 1

Input

11
7 1
7 2
7 3
7 4
7 5
7 6
7 7
1 1
34 14
84 19
1000000000 1000000000

Output

1
3
5
7
2
6
4
1
27
37
536870912

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback