#include <bits/stdc++.h>
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;
    if (n == 1) {
      cout << m << "\n";
      continue;
    }
    if (m == 0) {
      cout << 0 << "\n";
      continue;
    }
    if (n == m) {
      cout << (m * (m + 1)) / 2 << "\n";
      continue;
    }
    long long k = m % 2 ? n / 2 : n / 2 + 1;
    long long itr = 0, x = 2;
    long long ans = (k + 1) * (n - k);
    long long last = k, l = k, r = k;
    m--;
    for (long long i = k - 2; i >= 0 && i < n && m > 0; --m, ++itr) {
      if (itr % 2)
        ans += (i - r) * (n - i);
      else
        ans += (i + 1) * (l - i);
      last = i;
      l = min(i, l);
      r = max(r, i);
      if (itr % 2) {
        x += 2;
        i = k - x;
      } else
        i = k + x;
    }
    cout << ans + m << "\n";
  }
  return 0;
}
