#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k, r = 0;
    cin >> n >> k;
    if ((n % 2 != 0) && (k == 1)) {
      cout << (n / 2 + 1) * (n / 2 + 1) << endl;
      continue;
    }
    if (k > n / 2) {
      r = k - n / 2;
      k = n / 2;
    }
    r += 2 * k * (n - k);
    cout << r << endl;
  }
}
