#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    long long int N, M;
    cin >> N >> M;
    long long int rem = N - M;
    long long int mina = rem / (M + 1);
    rem = rem % (M + 1);
    if (M == 0)
      cout << 0 << endl;
    else
      cout << ((N * (N + 1)) / 2) -
                  (((mina * (mina + 1)) / 2) * (N - M - rem)) -
                  ((((mina + 1) * (mina + 2)) / 2) * rem)
           << endl;
  }
  return 0;
}
