#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()); roots[t] = sq; int max = t.size(); for (int j = 1; max + j <= 9; j++) { t.push_back('0'); 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()); if (roots[N]) std::cout << roots[N] << std::endl; else std::cout << -1 << std::endl; } }