#include <bits/stdc++.h>
using namespace std;
long long n, m, k, kk;
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (m == 0) {
      cout << 0 << endl;
      continue;
    }
    k = (n - m) / (m + 1);
    kk = (n - m) % (m + 1);
    cout << n * (n + 1) / 2 - kk * (k + 1) * (k + 2) / 2 -
                (m + 1 - kk) * k * (k + 1) / 2
         << endl;
  }
  return 0;
}
