#include using namespace std; using ll = long long; #ifdef LOCAL #include #else #define dbg(...) 0 #define dbgn(...) 0 #endif void solve(){ int n; cin >> n; vector v; while(n){ v.push_back(n % 10); n /= 10; } sort(v.begin(), v.end()); do{ int ans = 0, f = -1; for(int i = v.size() - 1; i >= 0; i--){ if(v[i] != 0 && f == -1) { f = i; } ans += v[i] * pow(10, i); } int rt = sqrt(ans); if(rt * rt == ans){ for(int i = f; i >= 0; i--){ cout << v[i]; } cout << '\n'; return; } } while(next_permutation(v.begin(), v.end())); cout << -1 << '\n'; } int32_t main() { cin.tie(0)->sync_with_stdio(0); int tt; cin >> tt; while(tt--){ solve(); } return 0; }