#include <bits/stdc++.h>
using namespace std;
long long n, x, k, z, m, pos, q, ans_v, y, mx;
long long x1, p1, x2;
long long a[100500];
vector<long long> f;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (m == 0) {
      cout << 0 << "\n";
    } else {
      long long x = (n - m) / (m + 1);
      long long y = (n - m) % (m + 1);
      cout << n * (n + 1) / 2 -
                  ((x * (x + 1) / 2) * (m + 1 - y) + y * (x + 1) * (x + 2) / 2)
           << "\n";
    }
  }
  return 0;
}
