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 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): if y in d: a = ''.join(sorted(str(N))) b = ''.join(sorted(str(d[y]))) if len(a) >= len(b) and a.endswith(b): ans = min(ans, d[y]) if ans == INF: return -1 return ans INF = 1 << 60 T = int(input()) for _ in range(T): res = solve() print(res)