#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int cases;
  cin >> cases;
  while (cases--) {
    long long n, m;
    cin >> n >> m;
    if (m == 0) {
      cout << "0\n";
      continue;
    }
    auto f = [](long long x) {
      if (x <= 0) return 0LL;
      long long ret = (x * x + x) / 2LL;
      return ret;
    };
    long long z = n - m;
    if (z <= m + 1) {
      cout << f(n) - z << "\n";
      continue;
    }
    long long blk = z / (m + (m == 1));
    long long bad = f(max(0LL, blk)) * m;
    long long rr = z - (m * blk);
    bad += f(max(0LL, rr));
    long long res = f(n) - bad;
    cout << res << "\n";
  }
}
