text: #include <bits/stdc++.h>
  
  using namespace std;
  
  /*************************************************************************************************************
   *                                              
   * Link : https://www.geeksforgeeks.org/rotate-a-matrix-by-90-degree-in-clockwise-direction-without-using-any-extra-space/
   * Description:
   *  Given a square matrix, turn it by 90 degrees in clockwise direction without using any extra space.
   * Resources:
   *  https://www.geeksforgeeks.org/rotate-a-matrix-by-90-degree-in-clockwise-direction-without-using-any-extra-space/
   * 
  *************************************************************************************************************/
  
  #define si(x) scanf("%d", &x)
  #define sll(x) scanf("%lld", &x)
  #define ss(s) getline(cin, s)
  #define pi(x) printf("%d\n", x)
  #define pll(x) printf("%lld\n", x)
  #define ps(s) cout << s << "\n"
  #define fo(i, n) for (int i = 0; i < n; i++)
  // #define fo(i, k, n) for (int i = k; k < n ? i < n : i >= n; k < n ? i++ : i--)
  #define ll long long
  #define deb(x) cout << #x << "=" << x << "\n"
  #define pb push_back
  #define mp make_pair
  #define f first
  #define s second
  #define all(x) x.begin(), x.end()
  #define clr(x) memset(x, 0, sizeof(x))
  #define sortall(x) sort(all(x))
  #define tr(a, it) for (auto it = a.begin(); it != a.end(); it++)
  #define present(c, x) (c.find(x) != c.end())
  #define cpresent(c, x) (find(all(c), x) != c.end())
  
  typedef pair<int, int> pii;
  typedef pair<ll, ll> pll;
  typedef vector<int> vi;
  typedef vector<ll> vl;
  typedef vector<string> vs;
  typedef vector<pii> vpii;
  typedef vector<pll> vpll;
  typedef vector<vi> vvi;
  typedef vector<vl> vvl;
  
  int main()
  {
      ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
      srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  
      int t{0};
      si(t);
  
      while (t--)
      {
          int n{0};
          si(n);
          int arr[n][n];
          fo(i, n) fo(j, n) si(arr[i][j]);
  
          for (int i = 0; i < n/2; i++) 
          {
              for (int j = i; j < n - i - 1; j++)
              {
                  int temp = arr[i][j];
                  arr[i][j] = arr[n - j - 1][i];
                  arr[n - j - 1][i] = arr[n - i - 1][n - j - 1];
                  arr[n - i - 1][n - j - 1] = arr[j][n - i - 1];
                  arr[j][n - i - 1] = temp;
              }
          }
  
          fo(i, n)
          {
              fo(j, n) cout << arr[i][j] << " ";
              cout << "\n";
          }
      }
  
      return 0;
  }