#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int ar[n + 1];
    if (n >= m)
      cout << m << endl;
    else {
      int a = m / n;
      for (int i = 1; i <= n; i++) {
        ar[i] = a;
      }
      int b = m % n;
      for (int i = 1; i <= n; i++) {
        if (b == 0) break;
        ar[i]++;
        --b;
      }
      long long int ans = 0;
      for (int i = 1; i <= n; i++) {
        ans += (ar[i] * ar[i]);
      }
      cout << ans << endl;
    }
  }
}
