#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e3 + 5;

int a[maxn][maxn][3], b[maxn][maxn];
int p[3], o[3], f[3];
string str;

void Main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            cin >> a[i][j][2];
            a[i][j][2]--;
            a[i][j][0] = i;
            a[i][j][1] = j;
        }

    for (int i = 0; i < 3; i++) f[o[i] = i] = 0;
    cin >> str;
    for (char c : str) {
        if (c == 'L') f[1]--;
        if (c == 'R') f[1]++;
        if (c == 'U') f[0]--;
        if (c == 'D') f[0]++;
        if (c == 'I') swap(f[1], f[2]), swap(o[1], o[2]);
        if (c == 'C') swap(f[0], f[2]), swap(o[0], o[2]);
    }

    for (int i = 0; i < 3; i++) f[i] = (f[i] % n + n) % n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < 3; k++) p[k] = (a[i][j][o[k]] + f[k]) % n;
            b[p[0]][p[1]] = p[2];
        }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) cout << b[i][j] + 1 << " \n"[j == n - 1];
    cout << '\n';
}

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

    int T;
    cin >> T;
    while (T--) Main();
    return 0;
}