Secret Letter

Interview

Time limit1sMemory limit128 MB

Summary
Given a ciphertext, undo a 90-degree clockwise rotation of a square grid to recover the original letter.
Level

Easy3 of 10

Topics
Implementation, Array, String, Math
Solved
No attempts yet

Problem

Seonyeong wrote a love letter to her boyfriend. To keep others from reading it, she encrypts the letter as follows.

  1. Write the letter's characters into a square grid, one character at a time, in row-major order (left to right, top to bottom), starting at the top-left cell.
  2. Rotate this square 90 degrees clockwise.
  3. Read the rotated square again in row-major order, from the first row to the last row, and concatenate everything into a single line. The resulting string is the ciphertext.

Seonyeong never uses spaces, so the length of the letter is always a perfect square.

For example, if the original letter is RosesAreRedVioletsAreBlue (length 25, i.e. a 5×5 square), the encrypted letter becomes eedARBtVrolsiesuAoReerles.

Given an encrypted letter, write a program that restores the original letter.

Input

The first line contains the number of test cases TT. TT is at most 100100.

Each of the next TT lines contains one encrypted letter. Each letter consists only of uppercase and lowercase English letters, and its length is at least 11, at most 1000010000, and always a perfect square.

Output

For each test case, print the restored original letter on its own line.

Examples1

  1. Example 1

    Input
    3
    RSTEEOTCP
    eedARBtVrolsiesuAoReerles
    EarSvyeqeBsuneMa
    
    Expected output
    TOPSECRET
    RosesAreRedVioletsAreBlue
    SquaresMayBeEven