#include <bits/stdc++.h>
using namespace std;
long long n, m, k, q;
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    if (m == 0) {
      cout << 0 << '\n';
      continue;
    }
    if ((n + 1) % (m + 1) == 0)
      k = (n + 1) / (m + 1);
    else
      k = (n + 1) / (m + 1) + 1;
    long long ans = k * ((m - 1) * (n + 1) - k * (m * (m - 1) / 2));
    long long r = n - (m - 1) * k;
    if (r & 1)
      ans += (r / 2 + 1) * (r / 2 + 1);
    else
      ans += (r / 2) * (r / 2 + 1);
    cout << ans << '\n';
  }
  return 0;
}
