#include <bits/stdc++.h>
const double eps = 1e-7, PI = 3.1415926;
const int N = 2e5 + 10;
using namespace std;
long long n, m, k, x, y, a[N], mx = -1, mn = 1e9, sum;
char c[N];
string s, s1, s2;
map<long long, long long> mp;
vector<long long> vec;
int main() {
  long long q = 1;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    x = m;
    for (int i = 1; i <= n; i++) a[i] = m / n;
    x -= n * (m / n);
    for (int i = 1; i <= n; i++) {
      if (x) a[i]++, x--;
      a[i] = a[i] * a[i];
      sum += a[i];
    }
    cout << sum << endl;
    sum = 0;
  }
  return 0;
}
