#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, p, m, k, t, temp, r;
  string a, b, c;
  cin >> t;
  for (long long jjj = 1; jjj <= t; jjj++) {
    cin >> n >> m;
    long long x = min(m, (n - 1) / 2);
    long long s = m - x, ex;
    k = (n - x) / (x + 1) + 1;
    r = (n - x) % (x + 1);
    if (s == 0)
      ex = 0;
    else if (s <= r + k - 1)
      ex = s * (s + 1) / 2;
    else
      ex = s - r - k + 1 + (r + k - 1) * (r + k) / 2;
    cout << x * (x + 1) / 2 * k * k + (n - k + 1 - r) * r + ex << "\n";
  }
}
