#include <bits/stdc++.h>
using namespace std;
long long int ways(long long int a) { return (a * (a + 1)) / 2; }
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);
    long long int rem2 = rem % (M + 1);
    cout << ways(N) - ways(mina) * (M + 1 - rem2) - ways(mina + 1) * rem2
         << endl;
  }
  return 0;
}
