#include using namespace std; int main() { map> mp; for (int i = 1; i * i <= 1e9; i++) { string s = to_string(i * i); sort(s.begin(), s.end()); mp[stoi(s)].push_back(i * i); if (stoi(s) < 0) { cout << s << endl; } } int T; cin >> T; while (T--) { int N; cin >> N; string s = to_string(N); sort(s.rbegin(), s.rend()); int cnt = 0; while (s.back() == '0') { s.pop_back(); cnt++; } reverse(s.begin(), s.end()); if (mp.contains(stoi(s))) { int ans = *min_element(mp[stoi(s)].begin(), mp[stoi(s)].end()); string t = to_string(ans); if (count(t.begin(), t.end(), '0') <= cnt) { cout << ans << endl; } else { cout << -1 << endl; } } else { cout << -1 << endl; } } return 0; }