#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
class CAyoubsFunction {
 public:
  void solve(std::istream &in, std::ostream &out) {
    long long n, m;
    in >> n >> m;
    n++;
    m++;
    long long max = 0;
    long long ans = 0;
    long long tt = (n * n - (n % m) * (((n + m - 1) / m) * ((n + m - 1) / m)) -
                    (m - (n % m)) * (n / m) * (n / m)) /
                   2;
    out << tt << endl;
  }
};
int main() {
  CAyoubsFunction solver;
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  int n;
  in >> n;
  for (int i = 0; i < n; ++i) {
    solver.solve(in, out);
  }
  return 0;
}
