#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> m >> n;
    long long int ans = (m * (m - 1)) / 2;
    m -= n;
    long long int nos = (m) / (n + 1);
    long long int rem = (m) % (n + 1);
    long long int p = n + 1 - rem;
    ans -= p * (nos * (nos - 1)) / 2;
    ans -= rem * ((nos + 1) * (nos)) / 2;
    ans += n;
    cout << ans << endl;
  }
}
