#include <bits/stdc++.h>
using namespace std;
int Gcd(int a, int b) {
  if (b == 0) return a;
  return Gcd(b, a % b);
}
int Lcm(int a, int b) { return a / Gcd(a, b) * b; }
inline long long read() {
  long long f = 1, x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int maxn = 1e6 + 10;
int main() {
  int T = read();
  while (T--) {
    long long n = read(), m = read();
    long long n0 = n - m, n1 = m;
    if (n0 == 0) {
      cout << n * (n + 1) / 2 << endl;
      continue;
    }
    long long num = n0 / (m + 1), left = n0 % (m + 1);
    long long val1 = (num + 1) * (num + 2) * left / 2;
    long long val2 = (n1 + 1 - left) * (num + 1) * num / 2;
    cout << n * (n + 1) / 2 - val1 - val2 << endl;
  }
  return 0;
}
