#include using namespace std; using ll = long long; #ifdef LOCAL #include #else #define debug(...) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int T; cin >> T; vector sq; vector> cnt; 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']++; sq.emplace_back(i * i); cnt.emplace_back(c); } auto solve = [&](int N) { string S = to_string(N); vector c(10); for (char x : S) c[x - '0']++; for (int i = 0; i < ssize(sq); i++) { bool ok = true; for (int j = 0; j < 10; j++) { if (c[j] == cnt[i][j]) continue; if (j == 0 && c[j] > cnt[i][j]) continue; ok = false; } if (ok) { return sq[i]; } } return -1; }; while (T--) { int N; cin >> N; cout << solve(N) << '\n'; } }