#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f;
long long n, m;
int main() {
  long long _;
  for (scanf("%lld", &_); _--;) {
    scanf("%lld%lld", &n, &m);
    long long ans;
    if (m == 0)
      ans = 0;
    else {
      long long shang = (n - m) / (m + 1);
      long long yu = (n - m) - shang * (m + 1);
      long long x = shang, y = shang + 1;
      long long nx = m + 1 - yu, ny = yu;
      ans = n * (n + 1) / 2 - x * x * nx - y * y * ny;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
