import sys input = sys.stdin.readline D=dict() for i in range(1,100000): x=i*i s=tuple(sorted(str(x))) if s in D: pass else: D[s]=x T=int(input()) for tests in range(T): X=tuple(sorted(input().strip())) ANS=-1 while True: if X in D: if ANS==-1: ANS=D[X] else: ANS=min(ANS,D[X]) if X[0]=="0": X=X[1:] else: break print(ANS)