# Arup Guha
# 12/14/2024
# Solution to Kattis Problem: Encoded Messages

# Rotate left - sq is a lists of lists, and a new list of lists is returned.
def rotLeft(sq):

    # Square size
    n = len(sq)

    res = []

    # Go backwards in columns
    for col in range(n-1,-1,-1):

        # Form word by reading down column.
        word = ""
        for row in range(n):
            word = word + sq[row][col]

        # Add to res.
        res.append(word)

    return res

# Silly but this will work given the small bounds.
# Normally I would binary search.
def getSqSize(s):
    i = 1
    while i*i < len(s):
        i += 1

    # They tell us this will work.
    return i

# Get cases.
nC = int(input())
for loop in range(nC):

    # Get input.
    s = input().strip()
    n = getSqSize(s)

    # Store the square here.
    sq = []
    for i in range(n):
        sq.append(s[i*n:(i+1)*n])

    # Rotate and print.
    sq = rotLeft(sq)
    for x in sq:
        print(x, end="")
    print()
