#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int mod = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, m;
    cin >> n >> m;
    int i, j;
    int z = n - m;
    long long ans = (n * (n + 1ll) >> 1ll);
    if (!m) {
      cout << "0\n";
      continue;
    }
    int c1, c2;
    j = z / (m + 1);
    c2 = (z - (j * (m + 1)));
    c1 = m + 1 - c2;
    ans -= c1 * 1ll * ((j * (j + 1ll)) >> 1ll);
    ans -= c2 * 1ll * (((j + 1ll) * (j + 2ll)) >> 1ll);
    cout << ans << '\n';
  }
  return 0;
}
