#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
const long long M = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (long long w = 0; w < t; w++) {
    long long n, m;
    cin >> n >> m;
    long long k;
    k = n / 2;
    if (m >= k) {
      long long ans;
      ans = n * (n - 1) / 2 + m;
      cout << ans << endl;
      continue;
    }
    string s = "";
    for (long long i = 0; i < n; i++) s += '0';
    long double p;
    p = (n + 1) / (m + 1);
    for (long long i = 1; i < m + 1; i++) {
      s[(long long)p * i - 1] = '1';
    }
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      if (s[i] == '1') v.push_back(i + 1);
    }
    long long ans = 0;
    for (long long i = 0; i < m; i++) {
      if (i == 0) {
        ans += v[i] * (n - v[i] + 1);
      } else {
        ans += (v[i] - v[i - 1]) * (n - v[i] + 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
