#include using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(x) (x).begin(), (x).end() using ll = long long; const ll MOD = 998244353; #include using namespace atcoder; using mint = modint998244353; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll t;cin>>t; map,ll> mp; ll cnt=1; while(cnt*cnt<=1e9+1){ ll cnt2=cnt*cnt; string s=to_string(cnt2); vector tmp(10,0); for(auto c:s) tmp[c-'0']++; if(!mp.count(tmp)) mp[tmp]=cnt2; cnt++; } while(t--){ string n;cin>>n; vector tmp(10,0); for(auto c:n) tmp[c-'0']++; ll ans=LLONG_MAX; ll q=tmp[0]; rep(i,q+1){ if(mp.count(tmp)){ ans=min(ans,mp[tmp]); } tmp[0]--; } if(ans==LLONG_MAX) cout<<-1<<"\n"; else cout<