#include <bits/stdc++.h>
using namespace std;
long long t, m, n, k, q, ans;
int main() {
  cin >> q;
  while (q--) {
    cin >> n >> m;
    if (m == 0)
      cout << 0 << endl;
    else if (n / m == 1)
      cout << n * (n + 1) / 2 - n + m << endl;
    else {
      t = (n - m) / (m + 1);
      k = n - (t + 1) * m;
      t = t * (t + 1) / 2 * m;
      k = k * (k + 1) / 2;
      ans = n * (n + 1) / 2 - t - k;
      cout << ans << endl;
    }
  }
  return 0;
}
