#P1242D. Number Discovery

Number Discovery

Description

Ujan needs some rest from cleaning, so he started playing with infinite sequences. He has two integers nn and kk. He creates an infinite sequence ss by repeating the following steps.

  1. Find kk smallest distinct positive integers that are not in ss. Let's call them u1,u2,,uku_{1}, u_{2}, \ldots, u_{k} from the smallest to the largest.
  2. Append u1,u2,,uku_{1}, u_{2}, \ldots, u_{k} and i=1kui\sum_{i=1}^{k} u_{i} to ss in this order.
  3. Go back to the first step.

Ujan will stop procrastinating when he writes the number nn in the sequence ss. Help him find the index of nn in ss. In other words, find the integer xx such that sx=ns_{x} = n. It's possible to prove that all positive integers are included in ss only once.

The first line contains a single integer tt (1t1051 \le t \le 10^{5}), the number of test cases.

Each of the following tt lines contains two integers nn and kk (1n10181 \le n \le 10^{18}, 2k1062 \le k \le 10^{6}), the number to be found in the sequence ss and the parameter used to create the sequence ss.

In each of the tt lines, output the answer for the corresponding test case.

Input

The first line contains a single integer tt (1t1051 \le t \le 10^{5}), the number of test cases.

Each of the following tt lines contains two integers nn and kk (1n10181 \le n \le 10^{18}, 2k1062 \le k \le 10^{6}), the number to be found in the sequence ss and the parameter used to create the sequence ss.

Output

In each of the tt lines, output the answer for the corresponding test case.

Sample Input 1

2
10 2
40 5

Sample Output 1

11
12

Note

In the first sample, s=(1,2,3,4,5,9,6,7,13,8,10,18,)s = (1, 2, 3, 4, 5, 9, 6, 7, 13, 8, 10, 18, \ldots). 1010 is the 1111-th number here, so the answer is 1111.

In the second sample, s=(1,2,3,4,5,15,6,7,8,9,10,40,)s = (1, 2, 3, 4, 5, 15, 6, 7, 8, 9, 10, 40, \ldots).