#include<bits/stdc++.h>
#define FOR(a,b,c) for(int a=(int)(b);a<=(int)(c);a++)
using namespace std;

const int N = 1005, M = 1e5+5;

int n, m;
int a[N][N],b[N][N];
char op[M];
int p[4], offset[4];

void go () {
	scanf("%d%d", &n, &m);
	FOR(i,0,n-1)FOR(j,0,n-1)scanf("%d",&a[i][j]), a[i][j]--;
	scanf("%s",op+1);
	FOR(i,1,3) p[i] = i, offset[i] = 0;
	FOR(i,1,m)switch(op[i]){
		case 'R': offset[p[2]]++; break;
		case 'L': offset[p[2]]--; break;
		case 'D': offset[p[1]]++; break;
		case 'U': offset[p[1]]--; break;
		case 'I': swap(p[2],p[3]); break;
		case 'C': swap(p[1],p[3]); break;
		default: assert(0);
	}
	FOR(i,1,3)offset[i] %= n;
	FOR(i,0,n-1)FOR(j,0,n-1){
		int t[] = {0, i, j, a[i][j]};

		b[(t[p[1]]+offset[p[1]]+n)%n][(t[p[2]]+offset[p[2]]+n)%n] 
			= (t[p[3]]+offset[p[3]]+n)%n;
		/*printf("(%d,%d,%d) => (%d,%d,%d)\n",
				i,j,a[i][j],
		(t[p[1]]+offset[p[1]]+n)%n,(t[p[2]]+offset[p[2]]+n)%n,
			(t[p[3]]+offset[p[3]]+n)%n);*/

	}
	FOR(i,0,n-1)FOR(j,0,n-1){
		printf("%d%c",b[i][j]+1," \n"[j==n-1]);
	}
	puts("");
}
int main () {
	int t;
	scanf("%d",&t);
	FOR(i,1,t)go();
	return 0;
}
