#include <bits/stdc++.h>
using namespace std;
int tc;
long long n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> tc;
  while (tc--) {
    cin >> n >> m;
    long long one = m;
    long long zero = n - one;
    long long area = one + 1;
    long long each = zero / area;
    long long add = zero % area;
    long long orig = area - add;
    long long ans = n * (n + 1) / 2 - orig * each * (each + 1) / 2 -
                    add * (each + 1) * (each + 2) / 2;
    cout << ans << '\n';
  }
  return 0;
}
