#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
long long t, n, m;
long long ans;
long long P(long long a, long long b) {
  if (b == 0) return 1ll;
  if (b % 2) return a * P(a, b / 2) * P(a, b / 2);
  return P(a, b / 2) * P(a, b / 2);
}
int32_t main() {
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (m == 0) {
      cout << 0 << '\n';
      continue;
    }
    ans = (n * (n - 1)) / 2 + n;
    n -= m;
    if (n <= m + 1) {
      ans -= n;
      cout << ans << '\n';
      continue;
    }
    ans -= P((n / (m + 1)), (n - (n % (m + 1)))) *
           P((n / (m + 1)), (n - (n % (m + 1))));
    ans -= P((n / (m + 1)) + 1, (n % (m + 1))) *
           P((n / (m + 1)) + 1, (n % (m + 1)));
    cout << ans << '\n';
  }
  return 0;
}
