#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    long long n, m;
    cin >> n >> m;
    long long num = (n - m) / (m + 1);
    long long rem = (n - m) % (m + 1);
    long long zeros = ((num * (num + 1)) / 2) * (m + 1 - rem) +
                      rem * (((num + 1) * (num + 1 + 1)) / 2);
    cout << (n * (n + 1)) / 2 - zeros << endl;
  }
  return 0;
}
