#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 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;
    long long ans =
        n * (n + 1) / 2 - x * (x + 1) / 2 * nx - y * (y + 1) / 2 * ny;
    printf("%lld\n", ans);
  }
  return 0;
}
