#include <bits/stdc++.h>
using namespace std;
int main() {
  long long minsq, q, r;
  int t, n, m;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &m);
    q = (n - m) / (m + 1);
    r = (n - m) % (m + 1);
    minsq = (q + 1) * (q + 1) * r + q * q * (m + 1 - r);
    printf("%lld\n", ((long long)n * n + m - minsq) / 2);
  }
}
