#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int n) {
  if (x == 0) return 0;
  if (n == 0) return 1;
  if (n == 1) return x;
  if (n % 2 == 0) {
    long long int temp = power(x, n / 2);
    return (temp % 1000000007 * temp % 1000000007) % 1000000007;
  } else {
    long long int temp = power(x, n / 2);
    return (x % 1000000007 *
            ((temp % 1000000007 * temp % 1000000007) % 1000000007) %
            1000000007) %
           1000000007;
  }
}
long long int findroot(long long int i, long long int Arr[]) {
  while (Arr[i] != i) {
    Arr[i] = Arr[Arr[i]];
    i = Arr[i];
  }
  return i;
}
void weightedunion(long long int i, long long int j, long long int arr[],
                   long long int size[]) {
  long long int x = findroot(i, arr);
  long long int y = findroot(j, arr);
  if (size[x] > size[y]) {
    size[x] += size[y];
    arr[y] = arr[x];
  } else {
    size[y] += size[x];
    arr[x] = arr[y];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m, i, j, mid, ans;
    cin >> n >> m;
    long long int l = 1, h = n;
    while (l <= h) {
      mid = (l + h) / 2;
      if (mid * m <= n - mid + 1) {
        ans = mid;
        l = mid + 1;
      } else
        h = mid - 1;
    }
    cout << ((ans * (ans + 1)) / 2) * m + ((n - ans) * (n - ans + 1)) / 2
         << '\n';
  }
  return 0;
}
