#include <bits/stdc++.h>
using namespace std;
long long n, m;
int main() {
  int T;
  cin >> T;
  while (T--) {
    scanf("%lld%lld", &n, &m);
    long long ans = (n - m) * m + m * (m + 1) / 2;
    if (n - m > 1) {
      ans += (n - m) * ((n - m) - 1) / 2;
      if ((n - m) > m + 1) {
        long long x = n - m - m - 1;
        long long xx = x / (m + 1);
        long long yy = x % (m + 1);
        ans -= ((m + 1) - yy) * (1 + xx) * xx / 2;
        ans -= yy * ((2 + xx) * (xx + 2 - 1) / 2);
      }
    }
    if (m == 0)
      cout << "0\n";
    else
      cout << ans << endl;
  }
  return 0;
}
