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

int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};

int main(){

    int q; cin >> q;
    for(int nn = 0; nn < q; nn++){
        if(nn != 0) cout << endl;
        
        int h, w; cin >> h >> w;
        if(!h) break;

        int nowi, nowj, k;
        vector<vector<char> > s(h + 2, vector<char> (w + 2, '#'));
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> s[i][j];
                if(s[i][j] == '^'){
                    nowi = i;
                    nowj = j;
                    k = 0; 
                }else if(s[i][j] == '>'){
                    nowi = i;
                    nowj = j;
                    k = 1;
                }else if(s[i][j] == 'v'){
                    nowi = i;
                    nowj = j;
                    k = 2;
                }else if(s[i][j] == '<'){
                    nowi = i;
                    nowj = j;
                    k = 3;
                }
            }
        }

        int n; cin >> n;
        string op; cin >> op;
        for(int i = 0; i < n; i++){

            if(op[i] == 'U'){
                k = 0;
                int nexti = nowi + di[k];
                int nextj = nowj + dj[k];
                if(s[nexti][nextj] == '.'){
                    char tmp = s[nowi][nowj];
                    s[nowi][nowj] = s[nexti][nextj];
                    s[nexti][nextj] = tmp;
                    nowi = nexti;
                    nowj = nextj;
                }
            }

            if(op[i] == 'R'){
                k = 1;
                int nexti = nowi + di[k];
                int nextj = nowj + dj[k];
                if(s[nexti][nextj] == '.'){
                    char tmp = s[nowi][nowj];
                    s[nowi][nowj] = s[nexti][nextj];
                    s[nexti][nextj] = tmp;
                    nowi = nexti;
                    nowj = nextj;
                }
            }

            if(op[i] == 'D'){
                k = 2;
                int nexti = nowi + di[k];
                int nextj = nowj + dj[k];
                if(s[nexti][nextj] == '.'){
                    char tmp = s[nowi][nowj];
                    s[nowi][nowj] = s[nexti][nextj];
                    s[nexti][nextj] = tmp;
                    nowi = nexti;
                    nowj = nextj;
                }
            }

            if(op[i] == 'L'){
                k = 3;
                int nexti = nowi + di[k];
                int nextj = nowj + dj[k];
                if(s[nexti][nextj] == '.'){
                    char tmp = s[nowi][nowj];
                    s[nowi][nowj] = s[nexti][nextj];
                    s[nexti][nextj] = tmp;
                    nowi = nexti;
                    nowj = nextj;
                }
            }


            if(op[i] == 'S'){
                int bi = nowi + di[k];
                int bj = nowj + dj[k];

                while(1){
                    if(s[bi][bj] == '#') break;
                    else if(s[bi][bj] == '*'){
                        s[bi][bj] = '.';
                        break;
                    }

                    bi += di[k];
                    bj += dj[k];
                }
            }

            /*for(int i = 1; i <= h; i++){
                for(int j = 1; j <= w; j++){
                    cout << s[i][j];
                }
                cout << endl;
            }

            cout << endl;*/
        }
        
        if(k == 0){
            s[nowi][nowj] = '^';
        }else if(k == 1){
            s[nowi][nowj] = '>';            
        }else if(k == 2){
            s[nowi][nowj] = 'v';
        }else{
            s[nowi][nowj] = '<';
        }
        
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cout << s[i][j];
            }
            cout << endl;
        }
    }

    return 0;
}


Measured memory_bytes: 3028