#include <bits/stdc++.h>
using namespace std;
int T;
int n, m;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    if (m == 0) {
      cout << 0 << endl;
      continue;
    }
    long long ans = n * (n - 1) / 2;
    int mm = m * 2;
    int tmp = n - m;
    int xx = tmp / (m + 1);
    int tt = tmp % (m + 1);
    int a = tmp / 2, b = tmp - a;
    long long cnt = (m - tt) * (xx - 1) * xx / 2 + tt * (xx + 1) * xx / 2;
    ans -= cnt;
    ans += m;
    cout << ans << endl;
  }
  return 0;
}
