#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    long long int n, m;
    cin >> n >> m;
    long long int ans = n * (n + 1) / 2;
    if (m == 0) {
      cout << 0 << endl;
    } else if (n / 2 <= m) {
      ans -= (n - m);
      cout << ans << endl;
    } else {
      long long int p = n - m;
      m++;
      ans -= (p % m) * (p / m + 2) * (p / m + 1) / 2 +
             (m - p % m) * (p / m + 1) * (p / m) / 2;
      cout << ans << endl;
    }
  }
}
