#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, k, n, t, ans = 0, x, ct = 0, ln, m, md, zero, per;
  cin >> t;
  while (t--) {
    scanf("%lld%lld", &n, &m);
    zero = (n - m);
    per = zero / (m + 1);
    md = zero % (m + 1);
    ans = ((n * (n + 1)) / 2) - ((((per + 1) * (per + 2)) / 2) * md) -
          ((((per) * (per + 1)) / 2) * (m + 1 - md));
    printf("%lld\n", ans);
  }
}
