1. 
/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 11716 Digital Fortress
*
* @author Robert Reichart
*
* Status : Accepted
* Runtime: 0.432
*/

import java.util.*;
class Main{
public static void main(String... args){
Scanner in = new Scanner(System.in);
int cases=in.nextInt();

while(in.hasNext()){

String line = in.nextLine();
if (line.isEmpty()){continue;}

if (Math.sqrt(line.length())%1!=0){
System.out.println("INVALID");
}else{
SpecialPrintArray(CharInSQRTArray(line));
}
}
}

static char[][] CharInSQRTArray(String line){
int sqrt = (int)Math.sqrt(line.length());
char[][] letters = new char[sqrt][sqrt];
for (int i=0;i<sqrt;i++){
for (int j=0;j<sqrt;j++){
letters[i][j]=line.charAt(i*sqrt+j);
}
}
return letters;
}

static void SpecialPrintArray(char[][] letters){
StringBuilder out= new StringBuilder("");
for (int i=0;i<letters.length;i++){
for (int j=0;j<letters.length;j++){
out.append(letters[j][i]);
}
}
System.out.println(out);
}
}