#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long w = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    w = (w << 3) + (w << 1) + ch - 48;
    ch = getchar();
  }
  return w * f;
}
long long n, m, ans;
signed main() {
  long long T = read();
  while (T--) {
    n = read();
    m = read();
    ans = 0;
    long long l = (n - m) / (m + 1);
    long long res = n - m - (m + 1) * l;
    long long a = res;
    long long b = m + 1 - res;
    ans = n * (n + 1) / 2;
    ans -= (l + 2) * (l + 1) / 2 * a;
    ans -= (l + 1) * l / 2 * b;
    cout << ans << endl;
  }
  return 0;
}
