#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433;
const long long M = 1743;
long long t, n, a[100100], k, m, f, p, g, gg;
unsigned long long ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    p = (n - m) / (m + 1);
    g = (n - m) % (m + 1);
    if (g != 0) p++;
    if (g == 0) g = m;
    f = -(p + 1) * (p + 1);
    k = (p + 1) * (n - p);
    ans = (2 * k + (g - 1) * f) * g / 2;
    gg = g * (p + 1);
    p--;
    f = -(p + 1) * (p + 1);
    k = (p + 1) * (n - p - gg);
    ans += (2 * k + (m - g - 1) * f) * (m - g) / 2;
    cout << ans << "\n";
  }
}
