#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define debug(a) cout << #a ": " << a << endl
using namespace std;
typedef long long ll;
const int N = 1010;
int a[N][N], b[N][N], mov[3], loc[3];
int main(void){
    IOS;
    int T; cin >> T;
    while (T--){
        int n, m; cin >> n >> m;
        for (int i = 0; i < n; ++i){
            for (int j = 0; j < n; ++j){
                cin >> a[i][j];
                a[i][j]--;
            }
        }
        for (int i = 0; i < 3; ++i){
            mov[i] = 0;
            loc[i] = i;
        }
        string op; cin >> op;
        for (int i = 0; i < m; ++i){
            if (op[i] == 'D'){
                mov[0] = (mov[0] + 1) % n;
            }else if (op[i] == 'U'){
                mov[0] = (mov[0] - 1 + n) % n;
            }else if (op[i] == 'L'){
                mov[1] = (mov[1] - 1 + n) % n;
            }else if (op[i] == 'R'){
                mov[1] = (mov[1] + 1) % n;
            }else if (op[i] == 'I'){
                swap(loc[1], loc[2]), swap(mov[1], mov[2]);
            }else{
                swap(loc[0], loc[2]), swap(mov[0], mov[2]);
            }
        }
        for (int i = 0; i < n; ++i){
            for (int j = 0; j < n; ++j){
                int tmp[3] = {i, j, a[i][j]}, tp[3];
                for (int k = 0; k < 3; ++k){
                    tp[k] = (tmp[loc[k]] + mov[k]) % n;
                }
                b[tp[0]][tp[1]] = tp[2];
                //b[tmp[loc[0]]][tmp[loc[1]]] = tmp[loc[2]];
            }
        }
        for (int i = 0; i < n; ++i){
            for (int j = 0; j < n; ++j){
                cout << b[i][j] + 1 << ' ';
            }
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}