#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, p, m, k, t, temp;
  string a, b, c;
  cin >> t;
  for (long long jjj = 0; jjj < t; jjj++) {
    cin >> n >> m;
    if (m <= n / 2) {
      cout << 2 * m * (n - m) << "\n";
    } else {
      k = n / 2;
      cout << 2 * k * (n - k) + (m - k) << "\n";
    }
  }
}
