#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    if (m == 0) {
      cout << 0 << endl;
      continue;
    }
    long long ans = (n * (n + 1)) / 2;
    if (m == n) {
      cout << ans << endl;
      continue;
    }
    long long a = n / (m + 1), b = n % (m + 1);
    ans -= (b + 1) * ((a * (a + 1)) / 2);
    ans -= (m - b - 1) * ((a * (a - 1)) / 2);
    cout << ans << endl;
  }
}
