#include using namespace std; using ll = long long; #define rep(i, a, n) for(int i = a; i < n; i++) int main() { int T; cin >> T; using P = pair; map> mp; ll n = 1e10; for(ll i = 1; i * i < n; i++) { int sq = i * i; string s = to_string(sq); sort(s.begin(), s.end(), greater()); int z = 0; while(s.back() == '0') { z++; s.pop_back(); } mp[P(s, z)].push_back(sq); } for(auto it = mp.begin(); it != mp.end(); it++) { sort(it->second.begin(), it->second.end()); } rep(ti, 0, T) { string s; cin >> s; sort(s.begin(), s.end(), greater()); int z = 0; while(s.back() == '0') { z++; s.pop_back(); } int f = 0; rep(i, 0, z + 1) { if(mp.count(P(s, i))) { cout << mp[P(s, i)][0] << endl; f = 1; break; } } if(f == 0) cout << -1 << endl; } }