#include <bits/stdc++.h>
using namespace std;
bool solve(string& a, string& b, string& c) {
  int n = a.length();
  for (int i = 0; i < n; ++i) {
    if (a[i] != c[i] && b[i] != c[i]) return false;
  }
  return true;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    int k = (n + 1) / (m + 1);
    int b = (n + 1) - (m + 1) * k;
    int t = m + 1 - b;
    long long res = (long long)(k + 1) * (k + 1) * (b - 1) * b / 2;
    res += (long long)(k + 1) * b * t * k;
    res += (long long)k * k * (t - 1) * t / 2;
    cout << res << endl;
  }
  return 0;
}
