#include <bits/stdc++.h>
using namespace std;
const long long NR = 3e5 + 1e2;
const long long oo = 1e16 + 100;
const long long MOD = 1e9 + 7;
const long long MOD2 = 1e9 + 9;
long long t, n, m;
signed main() {
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (!m) {
      cout << "0\n";
      continue;
    }
    long long x = (n - m) / (m + 1);
    long long y = (n - m) % (m + 1);
    long long z = m + 1 - y;
    cout << n * (n + 1) / 2 - y * (x + 2) * (x + 1) / 2 - z * (x) * (x + 1) / 2
         << '\n';
  }
  return 0;
}
