#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; vector cnt_string(int x) { vector res(10); while (x > 0) { res[x % 10]++; x /= 10; } return res; } map, int> mp; void solve() { int x; cin >> x; auto v = cnt_string(x); int ans = 2e9; while (v[0] >= 0) { if (mp.count(v)) chmin(ans, mp[v]); v[0]--; } if (ans == 2e9) ans = -1; cout << ans << "\n"; } int main() { rep(i, 1, 4e4) { auto ri = cnt_string(i * i); if (!mp.count(ri)) mp[ri] = i * i; } int t = 1; cin >> t; while (t--) solve(); }