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