#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    long long n, m;
    cin >> n >> m;
    if (m == 0)
      cout << 0 << endl;
    else {
      long long second = ((n + 1) * n) / 2;
      long long k = (n - m) / (m + 1);
      long long r = (n - m) % (m + 1);
      long long uns = 0;
      long long x = r * (k + 1) * (k + 2) / 2;
      uns += x + (m + 1 - r) * k * (k + 1) / 2;
      cout << second - uns << endl;
    }
  }
}
