#include <bits/stdc++.h>
using namespace std;
long long n, a, b, c, d, sum, ax[10005] = {0}, ans;
long long pw(long long a, long long b) {
  long long ans = 0;
  for (int i = 1; i <= max((long long)1, a); i++) {
    ans = ans + ax[i] * ax[i];
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    c = b / a;
    d = b % a;
    for (int j = 1; j <= max((long long)1, a); j++) ax[j] = c;
    for (int j = 1; j <= max((long long)1, a); j++) {
      if (d == 0)
        break;
      else {
        ++ax[j];
        --d;
      }
    }
    ans = pw(a, b);
    cout << ans << "\n";
  }
}
