#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long int t, m, n;
  cin >> t;
  unsigned long long int zeroes;
  while (t--) {
    cin >> n >> m;
    zeroes = n - m;
    if (m == 0) {
      cout << 0 << endl;
      continue;
    }
    unsigned long long int total = (n * (n + 1)) / 2;
    unsigned long long int div = zeroes / (m + 1);
    unsigned long long int mod = zeroes % (m + 1);
    unsigned long long int s = m + 1 - mod;
    unsigned long long int x = s * (div * (div + 1)) / 2;
    unsigned long long int y = mod * ((div + 1) * (div + 2)) / 2;
    cout << total - x - y << endl;
  }
  return 0;
}
