#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf(" %d", &t);
  for (int i = 0; i < t; ++i) {
    long long n, k;
    scanf(" %lld %lld", &n, &k);
    if (i == 109) printf("%d %d\n", n, k);
    long long ones = k;
    long long ba = (n * (n + 1ll)) >> 1;
    if (ones + k >= n) {
      ones = n - k;
      printf("%lld\n", ba - ones);
      continue;
    }
    if (k == 1) {
      long long us1 = 0, us2 = 0;
      if (n & 1)
        us2 = us1 = n >> 1;
      else
        us1 = n >> 1, us2 = (n >> 1) - 1;
      long long le = ((us1 * (us1 + 1ll)) >> 1) + ((us2 * (us2 + 1ll)) >> 1);
      printf("%lld\n", ba - le);
      continue;
    }
    long long spa = (k + 1);
    long long see = (n - k) / spa;
    long long les = 0;
    if ((n - k) % spa) {
      long long le1 = (n - k + 1ll) - (see * (k - 1));
      long long le2 = (n - k + 1ll) - ((see + 1ll) * (k - 1));
      long long pos1, pos2;
      pos1 = pos2 = 0;
      pos1 += ((see * (see + 1ll)) >> 1) * (k - 1),
          pos2 += (((see + 1ll) * (see + 2ll)) >> 1) * (k - 1);
      if (le1 & 1) {
        long long temp = le1 >> 1;
        pos1 += (temp * (temp + 1ll));
      } else {
        long long temp1 = le1 >> 1, temp2;
        temp2 = temp1 - 1ll;
        pos1 +=
            ((temp1 * (temp1 + 1ll)) >> 1) + (((temp2) * (temp2 + 1ll)) >> 1);
      }
      if (le2 & 1) {
        long long temp = le2 >> 1;
        pos2 += (temp * (temp + 1ll));
      } else {
        long long temp1 = le2 >> 1, temp2;
        temp2 = temp1 - 1ll;
        pos2 +=
            ((temp1 * (temp1 + 1ll)) >> 1) + (((temp2) * (temp2 + 1ll)) >> 1);
      }
      les = min(pos1, pos2);
    } else
      les += ((see * (see + 1ll)) >> 1) * (k + 1ll);
    printf("%lld\n", ba - les);
  }
  return 0;
}
