#P1242D. Number Discovery
Number Discovery
Description
Ujan needs some rest from cleaning, so he started playing with infinite sequences. He has two integers and . He creates an infinite sequence by repeating the following steps.
- Find smallest distinct positive integers that are not in . Let's call them from the smallest to the largest.
- Append and to in this order.
- Go back to the first step.
Ujan will stop procrastinating when he writes the number in the sequence . Help him find the index of in . In other words, find the integer such that . It's possible to prove that all positive integers are included in only once.
The first line contains a single integer (), the number of test cases.
Each of the following lines contains two integers and (, ), the number to be found in the sequence and the parameter used to create the sequence .
In each of the lines, output the answer for the corresponding test case.
Input
The first line contains a single integer (), the number of test cases.
Each of the following lines contains two integers and (, ), the number to be found in the sequence and the parameter used to create the sequence .
Output
In each of the lines, output the answer for the corresponding test case.
Note
In the first sample, . is the -th number here, so the answer is .
In the second sample, .