#include using namespace std; using ll = long long; #ifdef LOCAL #include #else #define debug(...) #endif constexpr int INF = (1 << 30) - 1; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int T; cin >> T; map, int> mp; for (int i = 1; i * i <= 1e9 + 10; i++) { string S = to_string(i * i); vector c(10); for (char x : S) c[x - '0']++; mp[c] = i * i; } auto solve = [&](int N) { string S = to_string(N); vector c(10); for (char x : S) c[x - '0']++; int mz = c[0], ans = INF; for (int z = 0; z <= mz; z++) { c[0] = z; if (mp.contains(c)) { ans = min(ans, mp[c]); } } return ans == INF ? -1 : ans; }; while (T--) { int N; cin >> N; cout << solve(N) << '\n'; } }