#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int maxm = 100 + 10;
const int Move[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
const int Move_[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1};
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
int main() {
  int t;
  read(t);
  while (t--) {
    long long n, m;
    read(n);
    read(m);
    if (m * 2 <= n) {
      long long ans = 2 * m * (n - m);
      cout << ans << endl;
    } else {
      long long ans = 2 * (n / 2) * (n - n / 2);
      ans += m - n / 2;
      cout << ans << endl;
    }
  }
  return 0;
}
