#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(vector<T> &a, long long n) {
  T x;
  a.clear();
  for (long long i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
}
template <class T>
void write(vector<T> &a) {
  for (T x : a) cout << x << " ";
  cout << endl;
}
int T;
long long n, m;
long long first(long long n, long long m) {
  long long res = n * (n + 1) / 2;
  long long cnt = 0;
  for (int i = 0; i < n; i++)
    if (m & (1 << i)) {
      res -= cnt * (cnt + 1) / 2;
      cnt = 0;
    } else {
      cnt++;
    }
  res -= cnt * (cnt + 1) / 2;
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> T;
  while (T--) {
    cin >> n >> m;
    if (m * 2 + 1 >= n) {
      cout << n * (n + 1) / 2 - (n - m) << endl;
    } else {
      long long s1 = (n - m) / (m + 1);
      long long s2 = (n - m) % (m + 1);
      long long l = s2;
      long long second = (m + 1) - s2;
      cout << n * (n + 1) / 2 - l * (s1 + 1) * (s1 + 2) / 2 -
                  second * s1 * (s1 + 1) / 2
           << endl;
    }
  }
}
