for _ in range(int(input())):
n=int(input())
count=0
while n!=0:
for u in range(31,0,-1):
if n>=u**2:
n=n-u**2
count+=1
print(count)
for _ in range(int(input())):
n=int(input())
count=0
while n!=0:
for u in range(31,0,-1):
if n>=u**2:
n=n-u**2
count+=1
print(count)
I think you will find this video helpful in explaining the logic,
ICL1902 - YouTube
Here’s a python code that will solve this problem, hope you find this useful
# cook your dish here
import math
for i in range(int(input())):
n = int(input())
cnt = 0
while n > 0:
m = int(math.sqrt(n))
n -= m*m
cnt += 1
print(cnt)
Hope you found this useful!