t = int(input()) C = [{} for _ in range(10)] N = 10 ** 5 for i in range(1, N): now = i * i now = str(now) cnt = now.count("0") L = [j for j in now if j != "0"] L.sort() L = tuple(L) for j in range(cnt, 10): if not L in C[j]: C[j][L] = int(now) else: C[j][L] = min(C[j][L], int(now)) for _ in range(t): n = int(input()) cnt = str(n).count("0") L = [j for j in str(n) if j != "0"] L.sort() L = tuple(L) if L in C[cnt]: print(C[cnt][L]) else: print(-1)