#include #include using namespace std; using i32 = int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using f64 = long double; using p2 = pair; using el = tuple; using mint = atcoder::modint998244353; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } i64 pow(i64 x, i64 n, i64 m) { i64 res = 1; i64 t = x % m; while (n > 0) { if (n & 1) { res = res * t % m; } t = t * t % m; n >>= 1; } return res; } void _main() { i64 ttt; cin >> ttt; map, i64> mp; for (i64 i = 1; i * i <= 10000000000; i++) { i64 x = i * i; vector v; while (x > 0) { v.push_back(x % 10); x /= 10; } sort(v.begin(), v.end()); if (!mp.count(v)) mp[v] = 1e18; mp[v] = min(mp[v], i * i); } while (ttt--) { i64 n; cin >> n; vector v; while (n > 0) { v.push_back(n % 10); n /= 10; } i64 ans = 1e18; sort(v.begin(), v.end()); if (mp.count(v)) ans = mp[v]; reverse(v.begin(), v.end()); while (!v.empty() && v.back() == 0) { v.pop_back(); vector nv = v; reverse(nv.begin(), nv.end()); if (mp.count(nv)) ans = min(ans, mp[nv]); } cout << (ans == 1e18 ? -1 : ans) << "\n"; } }