#include <bits/stdc++.h>
using namespace std;
mt19937 rd(chrono ::steady_clock ::now().time_since_epoch().count());
long long Rand(long long l, long long h) { return l + rd() % (h - l + 1); }
template <typename T>
inline void read(T &x) {
  register char c;
  bool sign = 0;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-') sign = !sign;
  x = c - '0';
  for (c = getchar(); isdigit(c); c = getchar()) x = x * 10 + c - '0';
  if (sign) x = -x;
}
const int N = 1;
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long zero = n - m;
    long long sub = 0;
    if (zero > m) {
      long long height = zero / (m + 1);
      long long k = zero % (m + 1);
      sub = k * (height + 1) * height / 2 +
            (m + 1 - k) * height * (height - 1) / 2;
    }
    cout << m + n * (n - 1) / 2 - sub << '\n';
  }
  return 0;
}
