#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long val = 2 * m - 1, ans = 0;
    if (n >= val) {
      long long val1;
      val1 = val * (n - val);
      ans = val1 + val + 2 * (m - 1) * (m - 1);
      long long x1 = (n - val + 1) / 2;
      long long x2 = (n - val) / 2;
      if (x1 != 0 && x2 != 0) ans++;
    } else {
      long long x = n - m;
      ans += n;
      ans += 2 * (n * x - x * (x + 1));
      ans += ((n - 2 * x - 1) * (n - 2 * x)) / 2;
    }
    if (m == 0) ans = 0;
    cout << ans << "\n";
  }
  return 0;
}
