#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, m;
    scanf("%lld %lld", &n, &m);
    if (m == 0) {
      printf("%d\n", 0);
      continue;
    }
    if (n == m) {
      printf("%lld\n", n * (n + 1) / 2);
    }
    long long d = (n - m) / (m + 1) + 1;
    long long ost = (n - m) % (m + 1);
    if (ost) d++;
    long long sol = 0;
    long long kol = n - d + 1;
    sol = kol * (kol + 1) / 2;
    sol += (m + 1) * (d - 2) + m;
    printf("%lld\n", sol);
  }
  return 0;
}
