1 #include <string>
   2 #include <vector>
   3 #include <numeric>
   4 #include <iostream>
   5 #include <stdio.h>
   6 #include <math.h>
   7 using namespace std;
   8 
   9 #define sz size()
  10 #define cs c_str()
  11 #define pb push_back
  12 #define LET(x,a) typeof(a) x(a)
  13 #define FOR(i,a,b) for(LET(i,a);i!=(b);++i)
  14 #define EACH(it,v) FOR(it,(v).begin(),(v).end())
  15 #define REP(i,n) FOR(i,0,n)
  16 
  17 #define GI ({int t;scanf("%d",&t);t;})
  18 #define COUNT(f,x) ({int _=0;f _+=(x);_;})
  19 #define EXISTS(f,x) ({int _=0;f if(x) {_=1;break;}_;})
  20 #define ALL(f,x) (!EXISTS(f,!(x)))
  21 #define MIN(f,x) ({LL _=LINF;f _<?=(x);_;})
  22 #define MAX(f,x) (-MIN(f,-(x)))
  23 
  24 int customSum(int a, int b) {
  25 	return a+b*b;
  26 }
  27 
  28 int main() {
  29 	int cnt =  GI;
  30 	string s;
  31 	cnt++;
  32 	while(cnt--) {
  33 		getline(cin, s);
  34 		if (s.sz == 0) continue;
  35 		int len = s.sz;
  36 		int sq = sqrt(len);
  37 		if (sq*sq != len) {
  38 			printf("INVALID\n");
  39 			continue;
  40 		}
  41 		REP(i, sq) {
  42 			REP(j, sq) {
  43 				int pos = i+j*sq;
  44 				printf("%c", s[pos]);
  45 			}
  46 		}
  47 		printf("\n");
  48 	}
  49 }