#C2184. Spiral Matrix Traversal

    ID: 45472 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

You are given an n x n matrix. Your task is to return the elements of the matrix in spiral order. The spiral order starts at the top-left corner and moves to the right, then down, then left, and then up, repeating until all elements have been processed.

More formally, if the matrix is represented as \(A\) with indices \(A[i][j]\) (where \(0 \le i, j < n\)), you are required to output the sequence as:

[ \text{spiral}(A) = [, A[0][0], A[0][1], \dots, A[0][n-1], A[1][n-1], \dots, A[n-1][n-1], A[n-1][n-2], \dots, A[n-1][0], A[n-2][0], \dots ] ]

Read input from standard input (stdin) and write the result to standard output (stdout).

inputFormat

The first line contains an integer \(T\) indicating the number of test cases. Each test case begins with an integer \(n\) denoting the size of the square matrix. The next \(n\) lines each contain \(n\) space-separated integers representing the rows of the matrix.

For example, an input for a single test case might be:

1
3
1 2 3
4 5 6
7 8 9

outputFormat

For each test case, output a single line containing the elements of the matrix in spiral order separated by a space.

## sample
1
3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5

</p>