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