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