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