#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
inline void io_optimize() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
signed main() {
  io_optimize();
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long l = n - m;
    if (m == 0) {
      cout << 0 << '\n';
    } else if (l > m + 1) {
      long long d = l / (m + 1);
      long long md = l % (m + 1);
      long long clr = m + 1 - md;
      cout << n * (n + 1) / 2 - clr * d * (d + 1) / 2 -
                  md * (d + 1) * (d + 2) / 2
           << '\n';
    } else {
      cout << (n + 1) * n / 2 - l << '\n';
    }
  }
  return 0;
}
