#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int d = (n - m) / (m + 1);
    int s = (n - m) % (m + 1);
    long long ans = (long long)n * (n + 1) / 2 - d * (d + 1) / 2 * (m + 1 - s) -
                    (d + 1) * (d + 2) / 2 * s;
    cout << ans << "\n";
  }
  return 0;
}
