#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;
}
