#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = true;
template <class T>
void print1D(T v) {
  if (DEBUG) {
    for (auto e : v) cout << e << " ";
    cout << endl;
  }
}
template <class T>
void print1D(T v, int min_incl, int max_incl) {
  if (DEBUG) {
    for (int i = min_incl; i <= max_incl; i++) cout << v[i] << " ";
    cout << endl;
  }
}
template <class T>
void print2D(T v) {
  if (DEBUG)
    for (auto a : v) print1D(a);
}
template <class T>
void print2D(T v, int min_incl, int max_incl, bool square) {
  if (DEBUG) {
    for (int i = min_incl; i <= max_incl; i++) {
      if (square)
        print1D(v[i], min_incl, max_incl);
      else
        print1D(v[i]);
    }
  }
}
template <class T>
void printPair(T p) {
  if (DEBUG) cout << "(" << p.first << ", " << p.second << ")" << endl;
}
template <class T>
void printSet(T s) {
  if (DEBUG) {
    for (auto it = s.begin(); it != s.end(); ++it) cout << *it << " ";
    cout << endl;
  }
}
int main() {
  int t = 0;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    if (m == 0) {
      cout << "0" << endl;
      continue;
    }
    long long all_substr = (long long)n * (long long)(n + 1) / (long long)2;
    if (m == n) {
      cout << all_substr << endl;
      continue;
    }
    int zeros = n - m;
    int max_zero_segs = min(zeros, m + 1);
    pair<int, int> big_seg = {zeros / max_zero_segs + 1, zeros % max_zero_segs};
    pair<int, int> small_seg = {zeros / max_zero_segs,
                                max_zero_segs - big_seg.second};
    long long zero_substr =
        (long long)big_seg.second * (long long)big_seg.first *
            (long long)(big_seg.first + 1) / (long long)2 +
        (long long)small_seg.second * (long long)small_seg.first *
            (long long)(small_seg.first + 1) / (long long)2;
    long long one_substr = all_substr - zero_substr;
    cout << (long long)one_substr << endl;
  }
  return 0;
}
