#include <bits/stdc++.h>
void solve() {
  long long n, m, len1, len2, num1, num2, ans;
  std::cin >> n >> m;
  len1 = (n - m) / (m + 1);
  len2 = len1 + 1;
  num2 = (n - m) % (m + 1);
  num1 = m + 1 - num2;
  ans = n * (n + 1) / 2 - len1 * (len1 + 1) / 2 * num1 -
        len2 * (len2 + 1) / 2 * num2;
  std::cout << ans << '\n';
}
int main() {
  int t;
  std::cin >> t;
  while (t--) solve();
  return 0;
}
