#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
int count(string s) {
  vector<int> pre(s.length(), 0);
  pre[0] = (s[0] == '1');
  for (int i = 1; i < (int)s.length(); ++i) pre[i] = pre[i - 1] + (s[i] == '1');
  int ans = 0;
  for (int i = 0; i < (int)s.length(); ++i) {
    for (int j = i; j < (int)s.length(); ++j) {
      int x = pre[j];
      if (i) x -= pre[i - 1];
      if (x > 0) ans++;
    }
  }
  return ans;
}
long long choose2(long long n) { return (n * (n + 1)) / 2; }
int solve(long long n, long long m) {
  if (m == 0) return 0;
  if (m == 1)
    return choose2(n) - (choose2((n - 1) / 2) + choose2((n - 1) - (n - 1) / 2));
  long long w = n - m;
  long long d1 = w / (m + 1);
  long long d2 = w / m;
  long long not_take1 = choose2(d1) * m + choose2(w - m * d1);
  long long r2 = w - d2 * (m - 1);
  long long not_take2 =
      choose2(d2) * (m - 1) + choose2(r2 / 2) + choose2(r2 - r2 / 2);
  long long not_take = min(not_take1, not_take2);
  long long ev = choose2(n);
  return ev - not_take;
}
void tmain() {
  int n, m;
  cin >> n >> m;
  cout << solve(n, m) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) tmain();
}
