#include<bits/stdc++.h>

using namespace std;
struct PT {
  int x, y, z;
  PT() {}
  PT(int x, int y, int z) : x(x), y(y), z(z) {}
  PT(const PT &p) : x(p.x), y(p.y), z(p.z)    {}
};
PT operator +(PT a,PT b){
  return PT(a.x+b.x,a.y+b.y, a.z+b.z);
}
PT operator -(PT a,PT b){
  return PT(a.x-b.x,a.y-b.y, a.z-b.z);
}
PT operator *(PT a,int b){
  return PT(a.x*b,a.y*b, a.z*b);
}
PT operator /(PT a,int b){
  return PT(a.x/b,a.y/b, a.z/b);
}
PT operator %(PT a,int b){
  return PT(a.x%b,a.y%b, a.z%b);
}

ostream& operator << (ostream &os, PT P) {
    return os<<P.x<<" "<<P.y<<" "<<P.z;
}

const int N = 1005;
int ans[N][N], a[N][N];

const PT DX(1, 0, 0), DY(0, 1, 0), DZ(0, 0, 1);
vector<PT> vv;

int n, q;
PT O, OX, OY, OZ;
void print() {
//     cout<<O<<" -- "<<OX<<" --"<<OY<<" -- "<<OZ<<endl;

    for (PT P: vv) {
        PT Q = (O + OX*P.x + OY*P.y + OZ*P.z)%n;
        ans[Q.x][Q.y] = Q.z;
    }

    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cout<<ans[i][j]+1<<" ";
        }
        cout<<endl;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;

    while (t--) {
        cin>>n>>q;

        O = PT(0, 0, 0);
        OX = DX, OY = DY, OZ = DZ;
        PT N(n, n, n);

        vv.clear();
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                cin>>a[i][j];
                a[i][j]--;
                vv.push_back({i, j, a[i][j]});
            }
        }


        while (q--) {
            char c;
            cin>>c;
            if (c == 'D')            O = O + DX;
            else if (c == 'U')       O = O - DX;
            else if (c == 'L')       O = O - DY;
            else if (c == 'R')       O = O + DY;
            else if (c == 'I') {
                swap(O.y, O.z);
                swap(OX.y, OX.z);
                swap(OY.y, OY.z);
                swap(OZ.y, OZ.z);
            }
            else if (c == 'C') {
                swap(O.x, O.z);
                swap(OX.x, OX.z);
                swap(OY.x, OY.z);
                swap(OZ.x, OZ.z);
            }
            else    print();
            O = (O+N)%n;
        }

        print();
    }
}
