#include <bits/stdc++.h>
using namespace std;
void solve() {
  int N, M;
  scanf("%d%d", &N, &M);
  int X = N - M;
  long long R = X / (M + 1);
  int S = X % (M + 1);
  int T = (M + 1) - S;
  long long ans = (long long)N * (N + 1) / 2;
  ;
  ;
  ans -= R * (R + 1) / 2 * T;
  ans -= (R + 1) * (R + 2) / 2 * S;
  printf("%lld\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
  return 0;
}
