#include <bits/stdc++.h>
using namespace std;
long long cal(long long n) {
  long long keep = (n * (n + 1)) / 2;
  return keep;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long n, m;
    cin >> n >> m;
    long long kep = (n - m);
    long long cnt = (n - m) / (m + 1);
    long long mod = (n - m) % (m + 1);
    long long pek = (m + 1) - mod;
    long long ans = cal(n) - (pek * cal(cnt) + cal(cnt + 1) * mod);
    cout << ans << endl;
  }
  return 0;
}
