#include <bits/stdc++.h>
using namespace std;
int typetest;
inline void fastIOfileinput() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen(""
            ".inp",
            "r")) {
    freopen(
        ""
        ".inp",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  if (fopen(""
            ".in",
            "r")) {
    freopen(
        ""
        ".in",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  typetest = 1;
}
string s;
long long n, m;
inline void Enter() { cin >> n >> m; }
long long cal(long long v) { return v * (v + 1) / 2; }
inline void solve() {
  long long ans = 0;
  if (m <= n / 2) {
    long long c =
        (n - m) / (m + (long long)(m < n / 2 || (n % 2 != 0 && m == n / 2)));
    long long d =
        (n - m) % (m + (long long)(m < n / 2 || (n % 2 != 0 && m == n / 2)));
    ans += d * cal(c + 1LL) +
           ((m + (long long)(m < n / 2 || (n % 2 != 0 && m == n / 2))) - d) *
               cal(c);
  } else {
    ans += n - m;
  }
  cout << cal(n) - ans << "\n";
}
int main() {
  fastIOfileinput();
  if (typetest) {
    int t;
    cin >> t;
    while (t--) {
      Enter();
      solve();
    }
  } else {
    Enter();
    solve();
  }
}
