#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\n";
  err(++it, args...);
}
void solve() {
  int n, m, k, x;
  scanf("%d%d", &m, &n);
  k = (n % m);
  x = (n / m);
  long long int ans = 0;
  ans = (x + 1) * (x + 1);
  ans *= k;
  long long int temp;
  temp = x, temp *= x;
  temp *= (m - k);
  cout << ans + temp << "\n";
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
