#include using namespace std; using ll = long long; using a2 = array; using a3 = array; template void chmin(A &l, const A &r) { if (r < l) l = r; } template void chmax(A &l, const A &r) { if (l < r) l = r; } ll mod = 998244353; int main() { ios::sync_with_stdio(false); cin.tie(0); ll t; cin >> t; for (int tt = 0; tt < t; tt++) { string s; cin >> s; sort(s.begin(), s.end()); bool ok = false; do { ll n = stoll(s); for (ll i = sqrt(n) - 1; i <= sqrt(n) + 1; i++) { if (i * i == n) { ok = true; } } if (ok) { cout << n << endl; break; } } while (next_permutation(s.begin(), s.end())); if (!ok) cout << -1 << endl; } return 0; }