#include <bits/stdc++.h>
using namespace std;
long long inf = 1000000000000000000, mod = 1000000007;
long long funo(long long num) {
  long long res = 1;
  res = num * num;
  return res;
}
long long fune(long long num) {
  long long res = 1;
  res = num * (num + 1);
  return res;
}
int main() {
  long long t, num, num1, n, m, newn, num2;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    num = 0;
    num1 = 0;
    num2 = 0;
    if (m == 0) {
      cout << "0" << endl;
      continue;
    }
    if (m < (n / 2) + n % 2) {
      if (n & 1) {
        n--;
        num = fune(n / 2 + n % 2);
        if (n / 2 + n % 2 - num > 0) num -= fune(n / 2 + n % 2 - num);
      } else {
        n--;
        num = funo(n / 2 + n % 2);
        if (n / 2 + n % 2 - num > 0) num -= funo(n / 2 + n % 2 - num);
      }
      if (n & 1) {
        num2 = funo(m);
      } else {
        num2 = fune(m);
      }
      cout << num + num2 << endl;
      continue;
    }
    num = n / 2 + n % 2;
    newn = n - (n / 2 + n % 2);
    num = min(num, m);
    m -= num;
    if (n & 1) {
      num = funo(n / 2 + n % 2);
      if (n / 2 + n % 2 - num > 0) num -= funo(n / 2 + n % 2 - num);
    } else {
      num = fune(n / 2 + n % 2);
      if (n / 2 + n % 2 - num > 0) num -= fune(n / 2 + n % 2 - num);
    }
    if (m) {
      if (n & 1) {
        num1 = fune(newn);
        if (newn - m) num1 -= fune(newn - m);
      } else {
        num1 = funo(newn);
        if (newn - m) num1 -= funo(newn - m);
      }
    }
    newn -= m;
    if (n & 1) {
      num2 = funo(newn);
    } else {
      num2 = fune(newn);
    }
    cout << num1 + num + num2 << endl;
  }
}
