#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); constexpr int r = 32000; vector> a(r + 1); array cnt{}; for(int i = 1; i <= r; i++){ cnt.fill(0); int v = i * i; while(v){ int d = v / 10; cnt[v - d * 10]++; v = d; } for(int j = 9; j >= 0; j--) a[i].first = a[i].first * 10 + cnt[j]; a[i].second = i * i; } sort(a.begin(), a.end()); int T, v; cin >> T; while(T--){ cin >> v; ll hs = 0; cnt.fill(0); while(v){ int d = v / 10; cnt[v - d * 10]++; v = d; } for(int i = 9; i >= 0; i--) hs = hs * 10 + cnt[i]; int ans = 1 << 30; while(1){ if(hs == 0) break; int p = lower_bound(a.begin(), a.end(), make_pair(hs, -1)) - a.begin(); if(p < a.size() && a[p].first == hs){ ans = min(ans, a[p].second); } if(hs % 10 == 0) break; hs--; } if(ans >> 30) ans = -1; cout << ans << '\n'; } }