#include <bits/stdc++.h>
using namespace std;
long long int n, m;
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (m == 0)
      cout << 0 << endl;
    else {
      long long int k = floor(((n - m) * 1.0) / (m + 1));
      long long int tot = ((n) * (n + 1)) / 2;
      long long int re = (n - (m + k * (m + 1)));
      tot -= (k * (k + 1) * (m + 1)) / 2;
      if (re != 0) {
        tot += (k * (k + 1)) / 2;
        tot -= ((k + re) * (re + k + 1)) / 2;
      }
      cout << tot << endl;
    }
  }
}
