#include <bits/stdc++.h>
using namespace std;
struct piii { int x,y,z; };
#define pb push_back
const int N = 1005;
int a[N][N],ans[N][N],n,m,d[3],shift[3];
void solve()
{
    vector <piii> vc;
    string st;
    cin>>n>>m;
    for (int i=0;i<n;++i)
        for (int j=0;j<n;++j)
            scanf("%d",&a[i][j]),
            vc.pb({i,j,--a[i][j]});
    cin>>st;
    for (int i=0;i<3;++i) d[i]=i,shift[i]=0;
    for (auto ch:st)
    {
        if (ch=='I') swap(d[1],d[2]);
        if (ch=='C') swap(d[0],d[2]);
        if (ch=='L') --shift[d[1]];
        if (ch=='R') ++shift[d[1]];
        if (ch=='U') --shift[d[0]];
        if (ch=='D') ++shift[d[0]];
    }
    for (auto &it:vc)
    {
        it={(it.x+shift[0]%n+n)%n,(it.y+shift[1]%n+n)%n,(it.z+shift[2]%n+n)%n};
        int t[3]={it.x,it.y,it.z};
        it={t[d[0]],t[d[1]],t[d[2]]};
        ans[it.x][it.y]=it.z;
    }
    for (int i=0;i<n;++i)
        for (int j=0;j<n;++j)
            printf("%d%c",ans[i][j]+1," \n"[j==n-1]);
}
int main()
{
    int T=1;cin>>T;
    while (T--) solve();
    return 0;
}