import pypyjit pypyjit.set_param('max_unroll_recursion=-1') from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline def answer(): X,A = map(int,input().split()) L = math.ceil(math.sqrt(X)) INF = (1<<30) dp = [INF]*(A+1) dp[0] = 0 for i in range(1,A+1): dp[i] = min(k**2 + X + dp[i-k] for k in range(1,min(i,L)+1)) print(dp[A]) for _ in range(int(input())): answer()