#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long power(long long x, long long y) {
  long long res = 1;
  x = x % 998244353;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % 998244353;
    y = y >> 1;
    x = (x * x) % 998244353;
  }
  return res;
}
long long modInverse(long long n) { return power(n, 998244353 - 2); }
long long nCr(long long n, long long r) {
  if (r == 0) return 1;
  long long fac[n + 1];
  fac[0] = 1;
  for (long long i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % 998244353;
  return (fac[n] * modInverse(fac[r]) % 998244353 * modInverse(fac[n - r]) %
          998244353) %
         998244353;
}
void aksayushx() {
  long long n, m;
  cin >> n >> m;
  long long ones = m, zeros = n - m;
  if (zeros <= ones + 1) {
    cout << (n * (n + 1) / 2) - zeros << '\n';
    return;
  }
  if (ones == 0) {
    cout << 0 << '\n';
    return;
  }
  long long k = n / (ones + 1);
  long long extra = zeros - (ones + 1) * (k - 1);
  long long a = ones + 1 - extra;
  long long b = extra;
  long long ans =
      ((n * (n + 1)) / 2) - a * ((k * (k - 1)) / 2) - b * ((k * (k + 1)) / 2);
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) aksayushx();
  return 0;
}
