#include <bits/stdc++.h>
using namespace std;
void shikhar7s(long long int cas) {
  long long int n, m, i, ans = 0, x, y;
  cin >> n >> m;
  if (!m) {
    cout << 0 << "\n";
    return;
  }
  if (m >= (n / 2)) {
    if (n == 1)
      ans = 1;
    else if (n % 2) {
      x = n / 2;
      ans = (x * (x + 1)) / 2;
      ans *= 4;
    } else if (n % 2 == 0) {
      ans = (n * n) / 2;
    }
    ans += (m - (n / 2));
    cout << ans << "\n";
  } else {
    x = m;
    ans = (x * (x + 1)) / 2;
    ans *= 4;
    x = (n - m - m - 1) / (m + 1);
    ans += ((x * (x + 4) * m * (m + 1)) / 2);
    y = (n - m - m - 1) % (m + 1);
    ans += ((y * x * m) + (2 * y * m));
    ans += ((y * y) - y) / 2;
    cout << ans << "\n";
  }
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  long long int t = 1;
  cin >> t;
  long long int cas = 1;
  while (cas <= t) {
    shikhar7s(cas);
    cas++;
  }
  return 0;
}
