#include using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i) #define ALL(a) a.begin(), a.end() #define Sort(a) sort(a.begin(), a.end()) #define RSort(a) sort(a.rbegin(), a.rend()) typedef long long int ll; typedef long double ld; typedef vector vi; typedef vector vll; typedef vector vc; typedef vector vst; typedef vector vd; typedef pair P; const long long INF = 0x1fffffffffffffff; const long long MOD = 998244353; const long double PI = acos(-1); template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; } template inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; } template void in(T&... a){ (cin >> ... >> a); } void out(){ cout << '\n'; } template void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template void inGraph(vector>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } } template struct ConvexHullTrick{ deque> deq; ConvexHullTrick(): deq(){ } bool check(pair line1, pair line2, pair line3){ return (line2.first - line1.first) * (line3.second - line2.second) >= (line2.second - line1.second) * (line3.first - line2.first); } T f(pair line, T x){ return line.first * x + line.second; } void add(T a, T b){ pair p = {a, b}; while((int) deq.size() >= 2 && check(deq.at((int) deq.size() - 2), deq.at((int) deq.size() - 1), p)){ deq.pop_back(); } deq.push_back(p); } T query(T x){ while((int) deq.size() >= 2 && f(deq.at(0), x) >= f(deq.at(1), x)){ deq.pop_front(); } return f(deq.at(0), x); } }; ll t; void input(){ in(t); } void solve(){ while(t--){ ll x, a; in(x, a); vll dp(a + 1, INF); dp[0] = 0; ConvexHullTrick CHT; for(ll i = 0; i <= a; i++){ if(i >= 1){ chmin(dp[i], CHT.query(i) + i * i + x); } CHT.add(-2 * i, i * i + dp[i]); } out(dp[a]); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); input(); solve(); }