# 10**9 までの平方数を洗い出す pattern = {} # pattern[(1, 0, 2, 3, 1, 3, ...)] = 平方数 for i in range(1, 10**5): x = i*i if x > 10**9: break cnt = [0] * 9 # 1 ~ 9 の個数 for s in str(x): if s != '0': cnt[int(s) - 1] += 1 t = tuple(cnt) # 最小のものだけ if t not in pattern: pattern[t] = x #print(pattern) T = int(input()) for i in range(T): N = input() cnt = [0] * 9 for s in str(N): if s != '0': cnt[int(s) - 1] += 1 t = tuple(cnt) if t in pattern: print(pattern[t]) else: print('-1')