#include <bits/stdc++.h>
using namespace std;
long long f(long long n) {
  long long sum = n * (n + 1) / 2;
  return sum;
}
int main() {
  int test;
  cin >> test;
  while (test--) {
    long long n, m;
    cin >> n >> m;
    long long sum = f(n);
    long long zero = n - m;
    long long avg = (zero) / (m + 1);
    long long b = (zero) % (m + 1);
    cout << sum - f(avg) * (m + 1 - b) - f(avg + 1) * b << endl;
    ;
  }
}
