#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long INF = (long long)2e18;
const int L = 96;
long long id[L];
long long st[L];
long long skp[L];
long long sum[L];
int m;
long long mult(long long x, long long y) {
  if (INF / x < y) return INF;
  return min(x * y, INF);
}
void build(long long k) {
  m = 0;
  id[m] = 0;
  st[m] = 1;
  skp[m] = -1;
  sum[m] = mult(st[m], k) + k * (k - 1) / 2;
  if (skp[m] != -1) sum[m] += st[m] + k - skp[m];
  sum[m] = min(sum[m], INF);
  m++;
  while (sum[m - 1] != INF) {
    if (m >= L - 2) throw;
    long long nw = st[m - 1] + k + min(skp[m - 1], 0LL) + 1;
    long long z = sum[m - 1] - nw;
    long long holes = 0;
    if (m > 1) holes = id[m - 1] - id[m - 2] - 1;
    long long full = (z - holes) / k;
    id[m] = id[m - 1] + full + 1;
    st[m] = nw + full * k + holes;
    if (st[m] == sum[m - 1]) st[m]++;
    if (sum[m - 1] >= st[m] + k) throw;
    if (sum[m - 1] > st[m])
      skp[m] = sum[m - 1];
    else
      skp[m] = -1;
    sum[m] = mult(st[m], k) + k * (k - 1) / 2;
    if (skp[m] != -1) sum[m] += st[m] + k - skp[m];
    sum[m] = min(sum[m], INF);
    m++;
  }
}
pair<long long, long long> findLast(int pos, long long n, long long k) {
  if (sum[pos] > n) throw;
  if (sum[pos + 1] <= n) return make_pair(id[pos + 1], sum[pos + 1]);
  long long pp = (n - k * (k - 1) / 2) / k;
  if (pos == 0) {
    long long s = pp;
    s--;
    s -= s % k;
    s++;
    while (s / k >= id[pos + 1]) s -= k;
    long long sm = mult(s, k) + k * (k - 1) / 2;
    if (sm > n) throw;
    return make_pair(s / k, sm);
  }
  pair<long long, long long> cur = findLast(pos - 1, pp + k - 1, k);
  long long nw = st[pos] + k + min(skp[pos], 0LL) + 1;
  if (cur.first == id[pos - 1]) {
    if (pp < nw) return make_pair(id[pos], sum[pos]);
    long long curId = id[pos] + 1;
    long long full = (pp - nw) / k;
    curId += full;
    nw += full * k;
    return make_pair(curId, nw * k + k * (k - 1) / 2);
  }
  long long holes = cur.first - id[pos - 1];
  long long z = (cur.second - nw);
  long long full = (z - holes + 1) / k;
  long long curId = id[pos] + full;
  nw += holes - 1 + full * k;
  if (nw > cur.second) throw;
  if (nw + k <= cur.second) throw;
  long long oldNw = nw;
  if (nw == cur.second) nw++;
  long long curSum = mult(nw, k) + k * (k - 1) / 2;
  if (nw <= cur.second) curSum += nw + k - cur.second;
  if (curSum > n) {
    if (full == 0) return make_pair(id[pos], sum[pos]);
    nw = oldNw;
    nw -= k;
    curSum = mult(nw, k) + k * (k - 1) / 2;
    if (curSum > n) throw;
    return make_pair(curId, curSum);
  }
  curId++;
  if (nw <= cur.second) nw++;
  nw += k;
  if (mult(nw, k) + k * (k - 1) / 2 > n) return make_pair(curId, curSum);
  curId++;
  curSum = mult(nw, k) + k * (k - 1) / 2;
  full = (n - curSum) / (k * k);
  curId += full;
  curSum += full * k * k;
  return make_pair(curId, curSum);
}
long long solve(long long n, long long k) {
  build(k);
  int pos = m - 1;
  while (st[pos] > n) pos--;
  if (n == sum[pos]) return (k + 1) * (id[pos] + 1);
  if (n == skp[pos]) return (k + 1) * (id[pos - 1] + 1);
  long long zz = n - st[pos] + 1;
  if (skp[pos] != -1 && n >= skp[pos]) zz--;
  if (zz <= k) return (k + 1) * id[pos] + zz;
  if (pos == 0) {
    long long full = (n - 1) / k;
    return full * (k + 1) + (n - 1) % k + 1;
  }
  pair<long long, long long> ans = findLast(pos - 1, n, k);
  if (ans.second == n) {
    return (k + 1) * (ans.first + 1);
  }
  long long nw = st[pos] + k + min(skp[pos], 0LL) + 1;
  long long holes = ans.first - id[pos - 1];
  long long z = n - nw;
  long long full = (z - holes) / k;
  long long myId = id[pos] + full + 1;
  z = (z - holes) % k + 1;
  return myId * (k + 1) + z;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, k;
    scanf("%lld%lld", &n, &k);
    printf("%lld\n", solve(n, k));
  }
  return 0;
}

### Prompt

In CPP, your task is to solve the following problem:

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

  1. Find k smallest distinct positive integers that are not in s. Let's call them u_{1}, u_{2}, …, u_{k} from the smallest to the largest. 
  2. Append u_{1}, u_{2}, …, u_{k} and ∑_{i=1}^{k} u_{i} to s in this order. 
  3. Go back to the first step. 



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

Input

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

Each of the following t lines contains two integers n and k (1 ≤ n ≤ 10^{18}, 2 ≤ k ≤ 10^{6}), the number to be found in the sequence s and the parameter used to create the sequence s.

Output

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

Example

Input


2
10 2
40 5


Output


11
12

Note

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

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long INF = (long long)2e18;
const int L = 96;
long long id[L];
long long st[L];
long long skp[L];
long long sum[L];
int m;
long long mult(long long x, long long y) {
  if (INF / x < y) return INF;
  return min(x * y, INF);
}
void build(long long k) {
  m = 0;
  id[m] = 0;
  st[m] = 1;
  skp[m] = -1;
  sum[m] = mult(st[m], k) + k * (k - 1) / 2;
  if (skp[m] != -1) sum[m] += st[m] + k - skp[m];
  sum[m] = min(sum[m], INF);
  m++;
  while (sum[m - 1] != INF) {
    if (m >= L - 2) throw;
    long long nw = st[m - 1] + k + min(skp[m - 1], 0LL) + 1;
    long long z = sum[m - 1] - nw;
    long long holes = 0;
    if (m > 1) holes = id[m - 1] - id[m - 2] - 1;
    long long full = (z - holes) / k;
    id[m] = id[m - 1] + full + 1;
    st[m] = nw + full * k + holes;
    if (st[m] == sum[m - 1]) st[m]++;
    if (sum[m - 1] >= st[m] + k) throw;
    if (sum[m - 1] > st[m])
      skp[m] = sum[m - 1];
    else
      skp[m] = -1;
    sum[m] = mult(st[m], k) + k * (k - 1) / 2;
    if (skp[m] != -1) sum[m] += st[m] + k - skp[m];
    sum[m] = min(sum[m], INF);
    m++;
  }
}
pair<long long, long long> findLast(int pos, long long n, long long k) {
  if (sum[pos] > n) throw;
  if (sum[pos + 1] <= n) return make_pair(id[pos + 1], sum[pos + 1]);
  long long pp = (n - k * (k - 1) / 2) / k;
  if (pos == 0) {
    long long s = pp;
    s--;
    s -= s % k;
    s++;
    while (s / k >= id[pos + 1]) s -= k;
    long long sm = mult(s, k) + k * (k - 1) / 2;
    if (sm > n) throw;
    return make_pair(s / k, sm);
  }
  pair<long long, long long> cur = findLast(pos - 1, pp + k - 1, k);
  long long nw = st[pos] + k + min(skp[pos], 0LL) + 1;
  if (cur.first == id[pos - 1]) {
    if (pp < nw) return make_pair(id[pos], sum[pos]);
    long long curId = id[pos] + 1;
    long long full = (pp - nw) / k;
    curId += full;
    nw += full * k;
    return make_pair(curId, nw * k + k * (k - 1) / 2);
  }
  long long holes = cur.first - id[pos - 1];
  long long z = (cur.second - nw);
  long long full = (z - holes + 1) / k;
  long long curId = id[pos] + full;
  nw += holes - 1 + full * k;
  if (nw > cur.second) throw;
  if (nw + k <= cur.second) throw;
  long long oldNw = nw;
  if (nw == cur.second) nw++;
  long long curSum = mult(nw, k) + k * (k - 1) / 2;
  if (nw <= cur.second) curSum += nw + k - cur.second;
  if (curSum > n) {
    if (full == 0) return make_pair(id[pos], sum[pos]);
    nw = oldNw;
    nw -= k;
    curSum = mult(nw, k) + k * (k - 1) / 2;
    if (curSum > n) throw;
    return make_pair(curId, curSum);
  }
  curId++;
  if (nw <= cur.second) nw++;
  nw += k;
  if (mult(nw, k) + k * (k - 1) / 2 > n) return make_pair(curId, curSum);
  curId++;
  curSum = mult(nw, k) + k * (k - 1) / 2;
  full = (n - curSum) / (k * k);
  curId += full;
  curSum += full * k * k;
  return make_pair(curId, curSum);
}
long long solve(long long n, long long k) {
  build(k);
  int pos = m - 1;
  while (st[pos] > n) pos--;
  if (n == sum[pos]) return (k + 1) * (id[pos] + 1);
  if (n == skp[pos]) return (k + 1) * (id[pos - 1] + 1);
  long long zz = n - st[pos] + 1;
  if (skp[pos] != -1 && n >= skp[pos]) zz--;
  if (zz <= k) return (k + 1) * id[pos] + zz;
  if (pos == 0) {
    long long full = (n - 1) / k;
    return full * (k + 1) + (n - 1) % k + 1;
  }
  pair<long long, long long> ans = findLast(pos - 1, n, k);
  if (ans.second == n) {
    return (k + 1) * (ans.first + 1);
  }
  long long nw = st[pos] + k + min(skp[pos], 0LL) + 1;
  long long holes = ans.first - id[pos - 1];
  long long z = n - nw;
  long long full = (z - holes) / k;
  long long myId = id[pos] + full + 1;
  z = (z - holes) % k + 1;
  return myId * (k + 1) + z;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, k;
    scanf("%lld%lld", &n, &k);
    printf("%lld\n", solve(n, k));
  }
  return 0;
}

```