#include <iostream>
#include <vector>
#include <string>

using namespace std;

int h, w;

vector<string> field;

enum dir_t {N, W, S, E};

int x, y;
dir_t dir;

void find_armor() {
    for (int i = 0; i < field.size(); i++) {
        for (int j = 0; j < field[i].size(); j++) {
            switch (field[i][j]) {
                case '^':
                    dir = N;
                    x = j, y = i;
                    break;
                case '>':
                    dir = W;
                    x = j, y = i;
                    break;
                case 'v':
                    dir = S;
                    x = j, y = i;
                    break;
                case '<':
                    dir = E;
                    x = j, y = i;
                    break;
            }
        }
    }
}

void up() {
    dir = N;
    if (0 < y && field[y-1][x] == '.') {
        field[y][x] = '.';
        field[y-1][x] = '^';
        y--;
    } else {
        field[y][x] = '^';
    }
}

void down() {
    dir = S;
    if (y < h-1 && field[y+1][x] == '.') {
        field[y][x] = '.';
        field[y+1][x] = 'v';
        y++;
    } else {
        field[y][x] = 'v';
    }
}

void right() {
    dir = W;
    if (x < w-1 && field[y][x+1] == '.') {
        field[y][x] = '.';
        field[y][x+1] = '>';
        x++;
    } else {
        field[y][x] = '>';
    }
}

void left() {
    dir = E;
    if (0 < x && field[y][x-1] == '.') {
        field[y][x] = '.';
        field[y][x-1] = '<';
        x--;
    } else {
        field[y][x] = '<';
    }
}

void shoot() {
    switch (dir) {
        case N:
            for (int i = y-1; i >= 0; i--) {
                if (field[i][x] == '#') break;
                if (field[i][x] == '*') {
                    field[i][x] = '.';
                    break;
                }
            }
            break;
        case W:
            for (int i = x+1; i < w; i++) {
                if (field[y][i] == '#') break;
                if (field[y][i] == '*') {
                    field[y][i] = '.';
                    break;
                }
            }
            break;
        case S:
            for (int i = y+1; i < h; i++) {
                if (field[i][x] == '#') break;
                if (field[i][x] == '*') {
                    field[i][x] = '.';
                    break;
                }
            }
            break;
        case E:
            for (int i = x-1; i >= 0; i--) {
                if (field[y][i] == '#') break;
                if (field[y][i] == '*') {
                    field[y][i] = '.';
                    break;
                }
            }
            break;
    }
}

void do_cmd(char cmd) {
    switch (cmd) {
        case 'U':
            up();
            break;
        case 'D':
            down();
            break;
        case 'L':
            left();
            break;
        case 'R':
            right();
            break;
        case 'S':
            shoot();
            break;
    }
}

int main() {
    int T; cin >> T;
    for (int i = 0; i < T; i++) {
        cin >> h >> w;
        field.clear();
        for (int j = 0; j < h; j++) {
            string s; cin >> s;
            field.push_back(s);
        }

        int no_cmd; string cmd;
        cin >> no_cmd >> cmd;

        find_armor();
        for (int j = 0; j < no_cmd; j++) {
            do_cmd(cmd[j]);
        }

        for (int j = 0; j < field.size(); j++) {
            cout << field[j] << endl;
        }

        if (i != T-1) cout << endl;
    }
    return 0;
}

Measured memory_bytes: 0