#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int m, n1, n, k, p = 0, sum = 0;
    cin >> n >> k;
    map<int, int> t;
    n1 = n;
    p = k / n;
    k -= p * n;
    m = k % n;
    if (p != 0) sum += (n1 - m) * p * p;
    sum += m * (p + 1) * (p + 1);
    cout << sum << '\n';
  }
}
