#include <bits/stdc++.h>
using namespace std;
inline long long qread() {
  register char c = getchar();
  register long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - 48;
    c = getchar();
  }
  return x * f;
}
inline long long Abs(const long long& x) { return (x > 0 ? x : -x); }
inline long long Max(const long long& x, const long long& y) {
  return (x > y ? x : y);
}
inline long long Min(const long long& x, const long long& y) {
  return (x < y ? x : y);
}
inline long long Calc(long long n) { return n * (n - 1) / 2ll; }
signed main() {
  long long t = qread();
  while (t--) {
    register long long n = qread(), m = qread(), d;
    m = n - m;
    if (m == 0) {
      printf("%lld\n", Calc(n + 1));
      continue;
    }
    if (m == n) {
      puts("0");
      continue;
    }
    if (m > (n + 1) / 2)
      d = n + 1 - m;
    else
      d = m;
    register long long k = (m + d - 1) / d;
    register long long ans = (m / k) * Calc(k + 1) + Calc(m % k + 1);
    ans = min(ans, (m / k) * Calc(k) + Calc(m % k + m / k + 1));
    printf("%lld\n", Calc(n + 1) - ans);
  }
  return 0;
}
