#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
long long qpow(long long x, long long y, long long MOD) {
  long long a = 1;
  while (y) {
    if (y & 1) a = a * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return a;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    long long ans = 1LL * n * (n + 1) / 2;
    int w = n - m;
    int a = w / (m + 1), b = w % (m + 1);
    ans -= 1LL * b * (a + 2) * (a + 1) / 2;
    ans -= 1LL * (m + 1 - b) * (a + 1) * a / 2;
    cout << ans << endl;
  }
  return 0;
}
