from itertools import count LIMIT = 10**9 def f(n: int) -> list[str]: s = ''.join(sorted(str(n))) res = [s] while len(s) > 1 and s[0] == '0': s = s[1:] res.append(s) return res d = {} for i in count(1): x = i*i if x > LIMIT: break y = f(x) for y in f(x): if y not in d: d[y] = x def solve(): N = int(input()) ans = INF for y in f(N): ans = min(ans, d.get(y, INF)) if ans == INF: return -1 return ans INF = 1 << 60 T = int(input()) for _ in range(T): res = solve() print(res)