#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t, n, m, ex, ans, k;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    ans = n * (n + 1) / 2;
    k = (n - m) / (m + 1);
    ex = (n - m) % (m + 1);
    ans -= (k + 1) * (k + 2) / 2 * ex;
    ans -= k * (k + 1) / 2 * (m + 1 - ex);
    cout << ans << "\n";
  }
}
