#include using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rrep(i,n) for(int i=(int)(n-1);i>=0;i--) #define ALL(v) v.begin(),v.end() #define RALL(v) v.rbegin(),v.rend() template using V=vector; template using VV=V>; using u128=__int128_t; using ll=long long; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); map,int> m; for(int i=1;i*i<=1000000000;i++){ int j=i*i; V A(10); while(j){ A[j%10]++; j/=10; } if(m.contains(A)) continue; else m[A]=i*i; } int t; cin>>t; while(t--){ int n; cin>>n; V A(10); while(n){ A[n%10]++; n/=10; } int ans=1e9+7; if(m.contains(A)) ans=min(ans,m[A]); while(A[0]){ A[0]--; if(m.contains(A)) ans=min(ans,m[A]); } if(ans!=1e9+7) cout<