The pirates have just seized a giant gold coin!
To determine the ownership of this gold coin, they decided to select the owner using the following method:
Let the current number of remaining pirates be $$$n$$$. The pirates line up in a queue, and the pirates at positions $$$1, 1+k, 1+2k, \dots, 1+(\lceil\frac{n}{k}\rceil - 1) k$$$ are eliminated. This operation is repeated until only one pirate remains. The final remaining pirate will receive the gold coin.
Charlie is the smartest among the pirates. He wants to know where he should stand initially to be the last pirate remaining and win the coin.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 100$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n, k \leq 10^{12}$$$), representing the initial number of pirates and the parameter used for elimination.
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^{12}$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$10^{12}$$$.
For each test case, output a single integer on a new line, indicating the position of the pirate who will ultimately receive the gold coin in the initial queue.
46 28 310000 21919810 114514
4 8 8192 1919805
For the first test case in the example, the positions of the remaining pirates in the original sequence after each round are:
For the second test case in the example, the positions of the remaining pirates in the original sequence after each round are: