#include <iostream>
#include <string>
using namespace std;

const int dx[4] = { 0, 0, -1, 1 };
const int dy[4] = { -1, 1, 0, 0 };
const char mov[] = "UDLR";
const char tank[] = "^v<>";

int h, w, d, x, y;
string map[22], command;

int main()
{
    int t, n;
    cin >> t;
    map[0] = "######################";

    for(int i = 0; i < t; ++i)
    {
        cin >> h >> w;
        for(int j = 1; j <= h; ++j) {
            cin >> map[j];
            map[j] = "#" + map[j] + "#";
            for(int k = 0; k < 4; ++k)
                if(map[j].find(tank[k]) != string::npos)
                    y = j, x = map[j].find(tank[k]), d = k, map[y][x] = '.';
        }
        map[h+1] = map[0];
        cin >> n >> command;

        for(int j = 0; j < n; ++j)
        {
            for(int k = 0; k < 4; ++k)
                if(command[j] == mov[k]) {
                    d = k;
                    if(map[y+dy[d]][x+dx[d]] == '.')
                        x += dx[d], y += dy[d];
                }

            if(command[j] == 'S')
                for(int xx = x, yy = y; map[yy][xx] != '#'; xx += dx[d], yy += dy[d])
                    if(map[yy][xx] == '*') { map[yy][xx] = '.'; break; }
        }

        map[y][x] = tank[d];

        if(i) cout << endl;
        for(int j = 1; j <= h; ++j)
            cout << map[j].substr(1, w) << endl;
    }
}