#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 9;
const long long int p = 31;
const long long int inf = 1e14;
const long long int mod = 1e9 + 7;
long long int powm(long long int a, long long int b) {
  a = a % mod;
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (res % mod);
}
string s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int m, n;
    cin >> m >> n;
    if (n == 0) {
      cout << 0 << '\n';
    } else if ((m - n) > n) {
      long long int l = (m - 2 * n - 1) / 2 + 2;
      long long int ans =
          l * (m - l + 1) + (2 * (m - l + 1) * (n - 1)) - (2 * n * (n - 1));
      cout << ans << '\n';
    } else {
      long long int ex = n - m + n;
      long long int ans = m + 2 * m * (m - n - 1) - 2 * (m - n - 1) * (m - n);
      ans += ex + ex * (ex + 1) / 2;
      cout << ans << '\n';
    }
  }
}
