#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int sqrt(int);
int strlen(char *);
int main() {
  int t, i;
  char s[10005];
  scanf("%d", &t);
  getchar();
  for (i = 0; i < t; i++) {
    int j, len, root, k;
    gets(s);
    len = strlen(s);
    root = sqrt(len);
    if (root * root != len) {
      printf("INVALID\n");
      continue;
    }
    for (j = 0; j < root; j++) {
      for (k = j; k < len; k += root) {
        printf("%c", s[k]);
      }
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2740,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129a <main+0x13a>
lea    -0x2730(%rbp),%rdi
call   1060 <gets@plt>
lea    -0x2730(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2738(%rbp)
mov    -0x2738(%rbp),%edi
call   1080 <sqrt@plt>
mov    %eax,-0x273c(%rbp)
mov    -0x273c(%rbp),%eax
imul   -0x273c(%rbp),%eax
cmp    -0x2738(%rbp),%eax
je     11f7 <main+0x97>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128c <main+0x12c>
movl   $0x0,-0x2734(%rbp)
mov    -0x2734(%rbp),%eax
cmp    -0x273c(%rbp),%eax
jge    127e <main+0x11e>
mov    -0x2734(%rbp),%eax
mov    %eax,-0x2740(%rbp)
mov    -0x2740(%rbp),%eax
cmp    -0x2738(%rbp),%eax
jge    1265 <main+0x105>
movslq -0x2740(%rbp),%rax
movsbl -0x2730(%rbp,%rax,1),%esi
ff
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x273c(%rbp),%eax
add    -0x2740(%rbp),%eax
mov    %eax,-0x2740(%rbp)
jmp    121f <main+0xbf>
jmp    126a <main+0x10a>
mov    -0x2734(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2734(%rbp)
jmp    1201 <main+0xa1>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1190 <main+0x30>
xor    %eax,%eax
add    $0x2740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2728,%rsp
lea    0xe78(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <scanf@plt>
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <getchar@plt>
cmpl   $0x0,0xc(%rsp)
jle    1251 <main+0xe1>
lea    0x10(%rsp),%rbx
jmp    11ce <main+0x5e>
xchg   %ax,%ax
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x10(%rsp),%rbx
add    $0x1,%r12d
cmp    0xc(%rsp),%r12d
jge    1251 <main+0xe1>
mov    %rbx,%rdi
call   1070 <gets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    %eax,%edi
call   1090 <sqrt@plt>
mov    %eax,%ecx
imul   %eax,%ecx
cmp    %r13d,%ecx
jne    1240 <main+0xd0>
test   %eax,%eax
jle    11b0 <main+0x40>
mov    %eax,%ebx
movslq %r13d,%rbp
xor    %r15d,%r15d
jmp    1209 <main+0x99>
add    $0x1,%r15
cmp    %rbx,%r15
je     11b0 <main+0x40>
cmp    %r15d,%r13d
jle    1200 <main+0x90>
mov    %r15,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x10(%rsp,%r14,1),%edi
call   1030 <putchar@plt>
add    %rbx,%r14
cmp    %rbp,%r14
jl     1220 <main+0xb0>
jmp    1200 <main+0x90>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xdbc(%rip),%rdi
call   1040 <puts@plt>
jmp    11bf <main+0x4f>
xor    %eax,%eax
add    $0x2728,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c"INVALID\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [10005 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%10 = call i32 (...) @getchar()
store i32 0, i32* %3
br label %11
11: ; preds = %57, %0
%12 = load i32, i32* %3
%13 = load i32, i32* %2
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %60
15: ; preds = %11
%16 = getelementptr inbounds [10005 x i8], [10005 x i8]* %4, i64 0, i64 0
%17 = call i32 @gets(i8* %16)
%18 = getelementptr inbounds [10005 x i8], [10005 x i8]* %4, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = call i32 @sqrt(i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = load i32, i32* %7
%24 = mul nsw i32 %22, %23
%25 = load i32, i32* %6
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %57
29: ; preds = %15
store i32 0, i32* %5
br label %30
30: ; preds = %52, %29
%31 = load i32, i32* %5
%32 = load i32, i32* %7
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %55
34: ; preds = %30
%35 = load i32, i32* %5
store i32 %35, i32* %8
br label %36
36: ; preds = %47, %34
%37 = load i32, i32* %8
%38 = load i32, i32* %6
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [10005 x i8], [10005 x i8]* %4, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %40
%48 = load i32, i32* %7
%49 = load i32, i32* %8
%50 = add nsw i32 %49, %48
store i32 %50, i32* %8
br label %36
51: ; preds = %36
br label %52
52: ; preds = %51
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %30
55: ; preds = %30
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %55, %27
%58 = load i32, i32* %3
%59 = add nsw i32 %58, 1
store i32 %59, i32* %3
br label %11
60: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@str = [8 x i8] c"INVALID\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10005 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [10005 x i8], [10005 x i8]* %2, i64 0, i64 0
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%6 = call i32 (...) @getchar()
%7 = load i32, i32* %1
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %45
9: ; preds = %0, %41
%10 = phi i32 [ %42, %41 ], [ 0, %0 ]
%11 = call i32 @gets(i8* %4)
%12 = call i32 @strlen(i8* %4)
%13 = call i32 @sqrt(i32 %12)
%14 = mul nsw i32 %13, %13
%15 = icmp eq i32 %14, %12
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = icmp sgt i32 %13, 0
br i1 %17, label %18, label %39
18: ; preds = %16
%19 = zext i32 %13 to i64
%20 = sext i32 %12 to i64
%21 = zext i32 %13 to i64
br label %24
22: ; preds = %9
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %41
24: ; preds = %18, %36
%25 = phi i64 [ 0, %18 ], [ %37, %36 ]
%26 = trunc i64 %25 to i32
%27 = icmp sgt i32 %12, %26
br i1 %27, label %28, label %36
28: ; preds = %24, %28
%29 = phi i64 [ %34, %28 ], [ %25, %24 ]
%30 = getelementptr inbounds [10005 x i8], [10005 x i8]* %2, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = call i32 @putchar(i32 %32)
%34 = add i64 %29, %19
%35 = icmp slt i64 %34, %20
br i1 %35, label %28, label %36
36: ; preds = %28, %24
%37 = add nuw nsw i64 %25, 1
%38 = icmp eq i64 %37, %21
br i1 %38, label %39, label %24
39: ; preds = %36, %16
%40 = call i32 @putchar(i32 10)
br label %41
41: ; preds = %39, %22
%42 = add nuw nsw i32 %10, 1
%43 = load i32, i32* %1
%44 = icmp slt i32 %42, %43
br i1 %44, label %9, label %45
45: ; preds = %41, %0
ret i32 0
}