#include <bits/stdc++.h>
using namespace std;
long long solve() {
  long long ans = 0;
  int n, m;
  cin >> n >> m;
  if (m == 0) return ans;
  ans = n * (n + 1) / 2;
  if (m >= n / 2) {
    ans -= (n - m);
    return ans;
  }
  if (m == 1) {
    if (n % 2 != 0) {
      ans -= n / 2 * (n / 2 + 1);
      return ans;
    }
    ans -= (n / 2) * (n / 2 + 1) / 2;
    ans -= (n / 2 - 1) * (n / 2) / 2;
    return ans;
  }
  int x = n - m;
  int y = x / (m + 1);
  int rem = x % (m + 1);
  ans -= rem * ((y + 1) * (y + 2) / 2);
  ans -= (m + 1) * y * (y + 1) / 2;
  return ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << "\n";
  }
  return 0;
}
