#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-6;
const long long mod = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long x = n - m;
    long long z = (n * (n + 1)) / 2;
    long long c;
    long long cc = x / (m + 1);
    long long ccc = x % (m + 1);
    c = (m + 1 - ccc) * (cc * (cc + 1)) / 2 + ccc * ((cc + 1) * (cc + 2)) / 2;
    long long ans = z - c;
    cout << ans << endl;
  }
}
