#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const long long INF = 9223372036854775807;
const double efs = 1e-9;
const int mod = 1000000007;
long long min(long long a, long long b) { return a > b ? b : a; }
long long max(long long a, long long b) { return a > b ? a : b; }
int arr[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, m;
    scanf("%lld%lld", &n, &m);
    if (m == 0) {
      cout << 0 << endl;
    } else {
      long long cnt = n - m;
      long long ans = (1 + n) * n / 2;
      long long a = cnt / (m + 1);
      long long b = cnt % (m + 1);
      ans -= (1 + a) * a / 2 * (m + 1 - b);
      ans -= (1 + a + 1) * (a + 1) / 2 * b;
      cout << ans << endl;
    }
  }
  return 0;
}
