#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
long long gcd(long long a, long long b) {
  while (1) {
    if (a == 0) return b;
    b %= a;
    if (b == 0) return a;
    a %= b;
  }
}
long long pow(long long a, long long b) {
  long long re = 1, k = a;
  while (b != 0) {
    if (b % 2 == 1) re = (re * k) % 1000000007;
    k = (k * k) % 1000000007;
    b /= 2;
  }
  return re;
}
int main() {
  long long n, m, i, j, t;
  cin >> t;
  for (long long ii = 0; ii < t; ii++) {
    cin >> n >> m;
    vector<long long> a(n);
    if (m == 0) {
      cout << "0" << endl;
      continue;
    }
    if ((n + 1) / 2 - 1 == m) {
      for (i = 0; i < n; i++) {
        if (i % 2 == 1) a[i] = 1;
      }
    } else if ((n + 1) / 2 > m) {
      long long k = (n / m);
      i = (n - k * m) / 2;
      for (; i < n; i += k) {
        a[i] = 1;
      }
    } else {
      long long k = m - (n + 1) / 2;
      for (i = 0; i < n; i++) {
        if (i % 2 == 0)
          a[i] = 1;
        else if (k != 0) {
          a[i] = 1;
          k--;
        }
      }
    }
    long long s = 0;
    long long k = -1;
    for (i = 0; i < n; i++) {
      if (a[i] == 0) continue;
      s += (n - i);
      if (k == -1) {
        s += i * (n - i - 1) + i;
      } else {
        s += (i - k - 1) * (n - i - 1) + (i - k - 1);
      }
      k = i;
    }
    cout << s << endl;
  }
}
