#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
inline bool kthbit(long long n, int k) { return (n >> k) & 1; }
inline long long setkthbit(long long n, int k) { return n | (1ll << k); }
inline long long unsetkthbit(long long n, int k) { return n & ~(1ll << k); }
inline long long flipkthbit(long long n, int k) { return n ^ (1ll << k); }
long long my_log(long long n, int b) {
  long long i = 1;
  int ans = 0;
  while (1) {
    if (i > n) {
      ans--;
      break;
    }
    if (i == n) break;
    i *= b;
    ans++;
  }
  return ans;
}
long long pw(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long nc2(long long n) { return (1ll * n * (n - 1)) / 2; }
long long nsum(long long n) { return (1ll * n * (n + 1)) / 2; }
void Solve() {
  long long n, m, k, x, y, r, d, w, ans;
  cin >> n >> m;
  d = (n - m) / (m + 1);
  r = (n - m) % (m + 1);
  x = (m + 1 - r);
  w = x * nc2(d + 1) + r * nc2(d + 2);
  ans = nc2(n + 1) - w;
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int tests = 1;
  cin >> tests;
  while (tests--) {
    Solve();
  }
  return 0;
}
