#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
char dd[5] = ">v<^";

int main(){
    int rep;
    cin >> rep;
    for(int r=0; r<rep; r++){
        int h,w;
        cin >> h >> w;
        vector<vector<char> > field(h+2, vector<char>(w+2, '#'));
        int x, y, dir;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                char in;
                cin >> in;
                field[i][j] = in;
                if(in=='^' || in=='<' || in=='>' || in=='v'){
                    y = i;
                    x = j;
                    for(int k=0; k<4; k++){
                        if(in == dd[k]) dir=k;
                    }
                }
            }
        }

        int m;
        string manip;
        cin >> m >> manip;
        for(int i=0; i<m; i++){
            switch(manip[i]){
            case 'U':
                dir = 3;
                goto move;
            case 'D':
                dir = 1;
                goto move;
            case 'L':
                dir = 2;
                goto move;
            case 'R':
                dir = 0;
            move:
                if(field[y+dy[dir]][x+dx[dir]] == '.'){
                    field[y][x] = '.';
                    y += dy[dir];
                    x += dx[dir];
                    field[y][x] = dd[dir];
                }else{
                    field[y][x] = dd[dir];
                }
                break;
            case 'S':
                int nx=x, ny=y;
                while(field[ny][nx] != '#' && field[ny][nx] != '*'){
                    ny += dy[dir];
                    nx += dx[dir];
                }
                if(field[ny][nx] == '*'){
                    field[ny][nx] = '.';
                }
            }
        }

        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                cout << field[i][j];
            }
            cout << endl;
        }
        if(r<rep-1) cout << endl;
    }
    return 0;
}

