/* -*- coding: utf-8 -*- * * 3204.cc: No.3204 Permuted Integer - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_M = 10; /* typedef */ /* global variables */ int ps[MAX_M]; /* subroutines */ /* main */ int main() { int tn; scanf("%d", &tn); while (tn--) { int n; scanf("%d", &n); int m = 0; while (n > 0) ps[m++] = n % 10, n /= 10; sort(ps, ps + m); int minx = -1; do { int x = 0; for (int i = 0; i < m; i++) x = x * 10 + ps[i]; int y = sqrt(0.5 + x); if (y * y == x) { minx = x; break; } } while (next_permutation(ps, ps + m)); printf("%d\n", minx); } return 0; }