#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
const int MOD = 1e9 + 7;
int main() {
  int t = 0;
  scanf("%d", &t);
  while (t--) {
    long long n, m;
    scanf("%lld %lld", &n, &m);
    long long res1 = (n + 1) * (n + 1);
    long long M = res1 % (2 * (m + 1));
    long long ans = ((m) * (n + 1) * (n + 1)) / (2 * (m + 1));
    if (M == 0) {
      ans = m * (res1 / (2 * (m + 1)));
    } else {
      ans = m * ((res1 - M) / (2 * (m + 1))) + m * M / (2 * (m + 1));
    }
    ans = n * (n + 1) / 2 -
          (n - m) % (m + 1) * ((n - m) / (m + 1) + 1) *
              ((n - m) / (m + 1) + 2) / 2 -
          (m + 1 - (n - m) % (m + 1)) * ((n - m) / (m + 1)) *
              ((n - m) / (m + 1) + 1) / 2;
    printf("%lld\n", ans);
  }
  return 0;
}
