#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.second < b.second);
}
long long maxSubArraySum(long long a[], long long size) {
  long long max_so_far = INT_MIN, max_ending_here = 0;
  for (long long i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_so_far < max_ending_here) max_so_far = max_ending_here;
    if (max_ending_here < 0) max_ending_here = 0;
  }
  return max_so_far;
}
long long findSubarraySum(long long arr[], long long n, long long sum = 0) {
  unordered_map<long long, long long> prevSum;
  long long res = 0;
  long long currsum = 0;
  for (long long i = 0; i < n; i++) {
    currsum += arr[i];
    if (currsum == sum) res++;
    if (prevSum.find(currsum - sum) != prevSum.end())
      res += (prevSum[currsum - sum]);
    prevSum[currsum]++;
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long findGCD(long long arr[], long long n) {
  long long result = arr[0];
  for (long long i = 1; i < n; i++) result = gcd(arr[i], result);
  return result;
}
long long powmp(long long x, long long y) {
  long long res = 1;
  x = x;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    if (m >= (n + 1) / 2)
      cout << n * (n - 1) / 2 + m << endl;
    else if (m == 0)
      cout << 0 << endl;
    else {
      long long ans = 0;
      long long temp = n;
      long long div = (temp - m) / (m + 1);
      ans += n * m - m * (m - 1) / 2;
      if (div != 1)
        ans += div * (powmp(div, m - 1) - 1) / (div - 1);
      else
        ans += m * (m - 1) / 2;
      ans += (n - m - div * m) * (div * m);
      cout << ans << endl;
    }
  }
}
