#include <bits/stdc++.h>
using namespace std;
long long countstrings(long n, long m) {
  if (m == 0) return 0;
  if (m == n) return (long long)(n * (n + 1)) / 2;
  if (m == n - 1) return (long long)(n * (n + 1)) / 2 - 1;
  long d = (n - m) / (m + 1);
  long r = (n - m) % (m + 1);
  long long ineachdlengthstring = (long long)(d * (d + 1)) / 2;
  long long basestrings = ineachdlengthstring * (m + 1);
  long long excess = (long long)r * (d + 1);
  return (long long)(n * (n + 1)) / 2 - (basestrings + excess);
}
int main() {
  int T;
  cin >> T;
  cin.ignore();
  long n, m;
  for (int t = 0; t < T; ++t) {
    cin >> n >> m;
    cin.ignore();
    cout << countstrings(n, m) << endl;
  }
}
