#include #include #include #include static inline constexpr bool check(const uint_fast32_t target) noexcept { uint_fast32_t l = 0, r = UINT32_C(1) << 15; while (l + 1 < r) { const auto c = (l + r) >> 1; if (c * c <= target) l = c; else r = c; } return (l * l == target); } static inline constexpr int_fast32_t solve(std::string& N) noexcept { std::sort(N.begin(), N.end()); do { const auto target = std::stoull(N); if (check(target)) return target; } while (std::next_permutation(N.begin(), N.end())); return -1; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); uint_fast32_t T, i; std::cin >> T; for (i = 0; i != T; ++i) { std::string N; std::cin >> N; std::cout << solve(N) << '\n'; } return 0; }