#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(0);
const long long MAXN = 2e6 + 1, MOD = 998244353, M = 1000000001;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  for (; t--;) {
    long long n, m;
    cin >> n >> m;
    long long l = (n - m) / (m + 1);
    long long k = n - m - (m + 1) * l;
    long long ans = l * (l + 1) * (m + 1 - k) / 2 + k * (l + 1) * (l + 2) / 2;
    cout << n * (n + 1) / 2 - ans << '\n';
  }
  return 0;
}
