#include <bits/stdc++.h>
using namespace std;
int main() {
  int test, tc = 0;
  long long m, n, ans, i, j;
  cin >> test;
  while (test--) {
    ans = 0;
    cin >> n >> m;
    long long k = m, a, b;
    a = m / n;
    if (m % n != 0) a++;
    int tp = 0;
    for (j = 0; j < n - 1; j++) {
      if (k <= n - j) {
        tp = 1;
        ans += k;
        break;
      }
      ans += (a * a);
      k -= a;
      a = k / (n - j - 1);
      if (k % (n - j - 1) != 0) a++;
    }
    if (k <= n - j && tp == 0) {
      tp = 1;
      ans += k;
    }
    if (!tp) ans += k * k;
    cout << ans << endl;
  }
  return 0;
}
