#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);i>=0;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;

//Data
const int N=1000;
int n,m,a[N][N][3],b[N][N];
int p[3],o[3],f[3];
string str;

//Main
void Main(){
    cin>>n>>m;
    R(t,3) f[o[t]=t]=0;
    R(i,n)R(j,n){
        cin>>a[i][j][2],--a[i][j][2];
        a[i][j][0]=i,a[i][j][1]=j;
    }
    cin>>str;
    for(char c:str){
        if(c=='R') f[1]++;
        else if(c=='L') f[1]--;
        else if(c=='D') f[0]++;
        else if(c=='U') f[0]--;
        else if(c=='I') swap(o[1],o[2]),swap(f[1],f[2]);
        else if(c=='C') swap(o[0],o[2]),swap(f[0],f[2]);
    }
    R(i,3) (((f[i]%=n)+=n)%=n);
    R(i,n)R(j,n){
        R(t,3) p[t]=(a[i][j][o[t]]+f[t])%n;
        b[p[0]][p[1]]=p[2];
    }
    R(i,n)R(j,n) cout<<b[i][j]+1<<" \n"[n-j==1];
    cout<<'\n';  
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t; for(cin>>t;t--;Main());
    return 0;
}