#include <bits/stdc++.h>
using namespace std;
vector<long long> x;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long a;
  cin >> a;
  for (long long i = 0; i < a; i++) {
    long long p, q;
    cin >> p >> q;
    long long r = p - q;
    q++;
    long long m = r / q, n = r % q;
    long long f = (p * (p - 1)) / 2 + p;
    long long s = n * ((m + 1) * m) / 2 + (q - n) * (m * (m - 1)) / 2 + r;
    cout << f - s << "\n";
  }
  return 0;
}
