#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int T;
inline int Abs(int a) { return (a < 0 ? -a : a); }
long long n, m;
int main() {
  ios::sync_with_stdio(0);
  cin >> T;
  while (T--) {
    cin >> n >> m;
    if (m == 0)
      cout << 0 << endl;
    else {
      long long k = n / m;
      if (n % m == 0) k--;
      long long ans = (1LL + k) * k / 2;
      ans *= m;
      ans += (1LL + (n - k)) * (n - k) / 2;
      cout << ans << endl;
    }
  }
  return 0;
}
