#include <bits/stdc++.h>
using namespace std;
long long t, n, m, ans, tmp1, tmp2, tmp3;
long long read() {
  char cc = getchar();
  long long cn = 0, flus = 1;
  while (cc < '0' || cc > '9') {
    if (cc == '-') flus = -flus;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();
  return flus * cn;
}
signed main() {
  t = read();
  while (t--) {
    n = read(), m = read();
    ans = n * (n + 1) / 2, tmp1 = (n - m) / (m + 1),
    tmp2 = n - m - tmp1 * (m + 1), tmp3 = m + 1 - tmp2;
    swap(tmp2, tmp3), ++tmp1;
    if (tmp1) ans -= tmp2 * (tmp1 * (tmp1 - 1) / 2);
    ans -= tmp3 * (tmp1 * (tmp1 + 1) / 2);
    printf("%lld\n", ans);
  }
  return ~~(0 ^ 0 ^ 0);
}
