#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const long long int NEGINF = -1 * INF;
void solve() {
  long long int n, ones;
  cin >> n >> ones;
  long long int zeros = n - ones;
  long long int gaps = ones + 1;
  if (zeros % gaps == 0) {
    long long int zerosInEachGap = zeros / gaps;
    long long int ans = (n * (n + 1)) / 2 -
                        ((zerosInEachGap * (zerosInEachGap + 1)) / 2) * gaps;
    cout << ans << "\n";
  } else {
    long long int zerosInEachGap = zeros / gaps;
    long long int extraZeros = zeros - zerosInEachGap * gaps;
    long long int gap1Cnt = extraZeros;
    long long int gap2Cnt = gaps - gap1Cnt;
    long long int ans = (n * (n + 1)) / 2;
    ans -= (((zerosInEachGap + 2) * (zerosInEachGap + 1)) / 2) * gap1Cnt;
    ans -= ((zerosInEachGap * (zerosInEachGap + 1)) / 2) * gap2Cnt;
    cout << ans << "\n";
  }
}
int main() {
  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
