using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var t = NN; var ans = new int[t]; for (var u = 0; u < t; ++u) { var s = ReadLine(); var list = new int[s.Length]; ans[u] = int.MaxValue; Permutate(s.Length, perm => { var d = 0; for (var i = 0; i < perm.Length; ++i) d = d * 10 + s[perm[i]] - '0'; var sq = (int)Math.Sqrt(d); if (sq * sq == d) ans[u] = Math.Min(ans[u], d); }); if (ans[u] == int.MaxValue) ans[u] = -1; } WriteLine(string.Join("\n", ans)); } static void Permutate(int n, Action action) { var seed = new int[n]; for (var i = 0; i < n; ++i) seed[i] = i; var used = new bool[n]; var perm = new int[n]; Permutate(0, seed, perm, used, action); } static void Permutate(int pos, int[] seed, int[] perm, bool[] used, Action action) { if (pos == used.Length) action(perm); for (var i = 0; i < used.Length; ++i) { if (used[i]) continue; perm[pos] = seed[i]; used[i] = true; Permutate(pos + 1, seed, perm, used, action); used[i] = false; } } }