#include <bits/stdc++.h>
using namespace std;
const int maxx = 3000010;
const double eps = 1e-15;
const double pi = acos(-1.0);
unsigned long long t, n, m, u, v, s;
unsigned long long l = 0, r = 0, bj1 = 0, bj2 = 0, ans = 0, sum = 0, cnt = 0,
                   res = 0, opt = 0, len = 0, total = 0;
unsigned long long a[maxx], bj[maxx], bja[maxx], bjb[maxx];
int main() {
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld", &n, &m);
    unsigned long long len = (n - m) / (m + 1);
    unsigned long long num1 = (n - m - len * (m + 1)), num2 = m + 1 - num1;
    ans = (n * n - n) - (len * len + len) * num1 - (len * len - len) * num2;
    ans /= 2;
    ans += m;
    printf("%lld\n", ans);
  }
  return 0;
}
