#include <bits/stdc++.h>
using namespace std;
long long t, n, zero, one;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - 48;
    ch = getchar();
  }
  return s * w;
}
signed main() {
  t = read();
  while (t--) {
    long long ans;
    n = read();
    one = read();
    zero = n - one;
    if (one == 0) {
      printf("0\n");
      continue;
    }
    if (zero <= one) {
      ans = (n + 1ll) * n / 2ll;
      printf("%lld\n", ans - zero);
    } else {
      ans = 2 * (n - 1 + n - 1 - 2 * (n - 2 * one));
      printf("%lld\n", ans);
    }
  }
  return 0;
}
