#include using namespace std; #define rep(i, l, r) for (int i = (int)(l); i<(int)(r); i++) #define ll long long #define all(x) (x).begin(), (x).end() #define siz(x) (int)(x).size() const int inf = 1e9; const ll INF = 4e18; templatebool chmax(T &a, T b) { if (abool chmin(T &a, T b) { if (b di = {-1, 0, 1, 0}, dj = {0, -1, 0, 1}; void solve() { int T; cin >> T; map, ll> mp; for (ll x = 1; x * x <= 1e10; x++) { ll t = x * x; string s = to_string(t); array cnt; rep(i, 0, 10) cnt[i] = 0; for (char c : s) { cnt[c-'0']++; } if (!mp.contains(cnt)) { mp[cnt] = x * x; } } // cout << "ok" << endl; while(T--) { string N; cin >> N; array cnt; rep(i, 0, 10) cnt[i] = 0; for (char c : N) { cnt[c-'0']++; } // rep(i, 0, 10) cout << cnt[i] << " "; // cout << endl; //0の個数を全探索 ll ans = INF; rep(x, 0, 11) { if (mp.contains(cnt)) { chmin(ans, mp[cnt]); } cnt[0]--; } if (ans == INF) ans = -1; cout << ans << endl; } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int T = 1; // cin >> T; while(T--) { solve(); } }