#include <bits/stdc++.h>
using namespace std;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
constexpr long long TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T& t, const U& u) {
  if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
  if (t < u) t = u;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (int i = 0; i < (v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
void solve() {
  int n, m;
  cin >> n >> m;
  int b = (n - m) / (m + 1);
  int bg = (n - m) - b * (m + 1);
  int sm = m + 1 - bg;
  long long val = (long long)n * (n + 1) / 2 - (long long)b * (b + 1) / 2 * sm -
                  (long long)(b + 1) * (b + 2) / 2 * bg;
  cout << val << "\n";
}
int main() {
  int TC;
  cin >> TC;
  while (TC--) {
    solve();
  }
  return 0;
}
