#include #include #include #include int main() { std::unordered_map roots; for (int i = 1; i * i <= 1000000000; i++) { if (i % 10 == 0) continue; int sq = i * i; std::string s = std::to_string(sq); auto t = s; std::sort(t.rbegin(), t.rend()); if (!roots[t]) roots[t] = sq; } int T; std::cin >> T; while (T--) { std::string N; std::cin >> N; if (N.size() == 10) { std::cout << 1 << std::endl; continue; } std::sort(N.rbegin(), N.rend()); int result = -1; if (roots[N]) result = roots[N]; while (N.back() == '0') { N.pop_back(); if (roots[N]) result = roots[N]; } std::cout << result << std::endl; } }