#C9148. Rotate Matrix 90 Degrees Clockwise

    ID: 53209 Type: Default 1000ms 256MiB

Rotate Matrix 90 Degrees Clockwise

Rotate Matrix 90 Degrees Clockwise

Given an integer T representing the number of test cases. For each test case, an integer N is provided, representing the dimensions of an NxN matrix. Then N lines follow, each containing N space‑separated integers.

Your task is to rotate each matrix by 90° clockwise and output the rotated matrices. The transformation must be done in-place by first transposing the matrix and then reversing each row.

The rotation can be mathematically represented using the formula for the new indices: If an element is at position \( (i, j) \) in a matrix, after a 90° clockwise rotation it will move to \( (j, N-1-i) \).

inputFormat

The first line contains an integer T, the number of test cases. For each test case:

  • The first line contains an integer N, the size of the matrix.
  • The next N lines each contain N space-separated integers representing the matrix.

outputFormat

For each test case, output the rotated matrix. Each row should be printed on a separate line, and the rows of different test cases follow consecutively without extra blank lines.

## sample
2
3
1 2 3
4 5 6
7 8 9
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
7 4 1

8 5 2 9 6 3 13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4

</p>