#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
int n, m, f, g, t;
long long s;
int main() {
  cin >> t;
  for (int q = 0; q < t; q++) {
    cin >> n >> m;
    g = (n - m) / (m + 1);
    f = (n - m) % (m + 1);
    s = 1ll * (g + 2) * (f * (2 * n - f * (g + 2) - (g + 1) + 1)) / 2;
    s += 1ll * (m - f) * ((g + 1) * (n - (g + 2) * f - g + g + 1)) / 2;
    cout << s << '\n';
  }
}
