#include <bits/stdc++.h>
long long n, k, a[105];
long long get(long long pos, long long now, long long rk) {
  long long l = pos * (k * k + 1) + 1 + rk * k;
  if (l + k > now)
    return (l * 2 + k - 1) * k / 2 + (l <= now ? l + k - now : k);
  else
    return (l * 2 + k - 1) * k / 2;
}
long long find(long long id) {
  int top = 0;
  while (id) a[++top] = id % k, id /= k;
  long long pos = 0, now = k * (k + 1) / 2;
  while (top) now = get(pos, now, a[top]), pos = pos * k + a[top], top--;
  return now;
}
long long query1(long long n) {
  long long l = 0, r = (n - 1) / (k * k + 1), pos;
  while (l <= r) {
    long long mid = (l + r) / 2, lef = mid * (k * k + 1) + 1,
              rig = (mid + 1) * (k * k + 1);
    if ((lef * 2 + k - 1) <= n * 2 / k && (rig * 2 - k + 1) >= n * 2 / k) {
      pos = mid;
      break;
    } else if ((rig * 2 - k + 1) <= (n - 1) * 2 / k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  long long num = find(pos);
  l = 0, r = k - 1;
  while (l <= r) {
    long long mid = (l + r) / 2, w = get(pos, num, mid);
    if (w == n)
      return k * k * pos + (mid + 1) * k + mid + k * pos + 1;
    else if (w < n)
      l = mid + 1;
    else
      r = mid - 1;
  }
}
long long query2(long long n, long long num) {
  long long x = (n - 1) / (k * k + 1), s1 = x * k + (n - 1) % (k * k + 1) / k,
            s2 = x + (num < n);
  if (num <= n && (n - 1) % (k * k + 1) % k == 0) s1--;
  return n + s1 - s2;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%lld%lld", &n, &k);
    long long x = find((n - 1) / (k * k + 1));
    if (x == n)
      printf("%lld\n", query1(n));
    else
      printf("%lld\n", query2(n, x));
  }
  return 0;
}
