#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long num_z = n - m, num_o = m;
    long long rnd = num_z / (num_o + 1);
    long long res = num_z - rnd * (num_o + 1);
    long long ans = res * (rnd + 2) * (rnd + 1) / 2 +
                    (num_o + 1 - res) * rnd * (rnd + 1) / 2;
    cout << n * (n + 1) / 2 - ans << endl;
  }
  return 0;
}
