Hide

Problem J
Juggling Sequence

The Juggling Sequence is an integer sequence with n elements, defined as follow:

  • a1=1,

  • For every i1:

    • If aii, then ai+1=ai+i,

    • If ai>i, then ai+1=aii.

Let’s sort the juggling sequence in non-decreasing order. What is the m-th number?

Input

The first line of the input contains a single integer t (1t104) — the number of test cases.

t test cases follow, each test case contains a single line with two integers n and m (1mn1018).

Output

For each test case, print a single integer — the m-th number in the sorted juggling sequence.

Explanation of the sample input

With n=6, the juggling sequence is 1,2,4,1,5,10. After sorting, the sequence becomes 1,1,2,4,5,10.

Sample Input 1 Sample Output 1
3
6 1
6 2
6 6
1
1
10
Hide

Please log in to submit a solution to this problem

Log in