#include <bits/stdc++.h>
using namespace std;
int n, m;
void solve() {
  scanf("%d %d", &n, &m);
  long long ans = (long long)n * (n + 1) / 2;
  int mn = (n - m) / (m + 1);
  int rm = n - m - (m + 1) * mn;
  int cnt = m + 1 - rm;
  printf("%lld\n", ans - (long long)cnt * mn * (mn + 1) / 2 -
                       (long long)rm * (mn + 1) * (mn + 2) / 2);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
