#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 10;
const int inf = 0x3f3f3f3f;
long long m, n;
long long cal(long long x) { return x * (x + 1) / 2; }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld %lld", &n, &m);
    long long cnt = n - m;
    long long tot = cnt / (m + 1);
    long long g2 = cnt % (m + 1);
    long long g1 = m + 1 - g2;
    printf("%lld\n", cal(n) - g1 * cal(tot) - g2 * cal(tot + 1));
  }
  return 0;
}
