#include using namespace std; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } int main() { fast_io(); map mp; const int M = 1e9 + 5; for (int i = 1; i * i <= M; i++) { int num = i * i; string dig_cnt(10, '0'); int tot = 0; while (num > 0) { dig_cnt[num % 10]++; num /= 10; tot++; } while (tot < 10) { if (mp.find(dig_cnt) == mp.end()) { mp[dig_cnt] = i * i; } else { mp[dig_cnt] = min(mp[dig_cnt], i * i); } dig_cnt[0]++; tot++; } } int t; cin >> t; for (; t--;) { int n; cin >> n; string dig_cnt(10, '0'); while (n > 0) { dig_cnt[n % 10]++; n /= 10; } if (mp.count(dig_cnt)) { cout << mp[dig_cnt] << '\n'; } else { cout << -1 << '\n'; } } }