#include <bits/stdc++.h>
using namespace std;
long long int count(long long int n) {
  long long int w = (n * (n + 1));
  w /= 2;
  return w;
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m, i, j;
    cin >> n >> m;
    long long int w = n - m;
    long long int x = m + 1;
    if (m == 0) {
      cout << 0 << "\n";
      continue;
    }
    long long int ans = w / x;
    if (ans == 0)
      cout << count(n) - w << "\n";
    else {
      long long int xx = w % x;
      if (xx) {
        long long int k = n - m - m * ans - ans;
        cout << count(n) - k * count(ans + 1) - (m + 1 - k) * count(ans)
             << "\n";
      } else
        cout << count(n) - (n - m) << "\n";
    }
  }
}
