#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
inline bool equals(long double a, long double b) { return fabs(a - b) < 1e-9; }
inline long long gcd(long long a, long long b) {
  a = abs(a);
  b = abs(b);
  while (a > 0 && b > 0) (a > b ? a %= b : b %= a);
  return (a == 0 ? b : a);
}
inline long long power(long long x, long long n, long long m = LLONG_MAX) {
  long long res = 1;
  x = (x % m + m) % m;
  while (n) {
    if (n & 1) res = (res * x) % m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
long long go(long long m, long long d, long long l, long long ans) {
  for (long long i = 0; i < m; ++i) {
    ans -= ((d * (d + 1)) / 2);
  }
  ans -= ((l * (l + 1)) / 2);
  return ans;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long ans = ((n * (n + 1)) / 2);
  if (m >= n - m)
    ans -= (n - m);
  else if (m == 0)
    ans = 0;
  else {
    long long d1 = (n - m) / (m + 1);
    long long l1 = (n - m) % (m + 1);
    long long d2 = (n - m) / m;
    long long l2 = (n - m) % m;
    long long ans1 = go(m, d1, l1 + d1, ans);
    long long ans2 = go(m, d2, l2, ans);
    ans = max(ans1, ans2);
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  };
  return 0;
}
