#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) 
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int T;
int n, m, dammy;
string fi[51];
string str;

int main() {
    cin >> T;
    for (int AAA = 0; AAA < T; ++AAA) {
        cin >> n >> m;
        for (int i = 0; i < n; ++i) cin >> fi[i];
        cin >> dammy; cin >> str;
        
        int si = -1, sj = -1, sd = -1;
        for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
            if (fi[i][j] == '<') si = i, sj = j, sd = 3, fi[i][j] = '.';
            if (fi[i][j] == '>') si = i, sj = j, sd = 1, fi[i][j] = '.';
            if (fi[i][j] == '^') si = i, sj = j, sd = 2, fi[i][j] = '.';
            if (fi[i][j] == 'v') si = i, sj = j, sd = 0, fi[i][j] = '.';
        }
        for (int i = 0; i < str.size(); ++i) {
            if (str[i] == 'D') {
                int ni = si + dx[0], nj = sj + dy[0]; sd = 0;
                if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
                if (fi[ni][nj] != '.') continue;
                si = ni, sj = nj;
            }
            else if (str[i] == 'R') {
                int ni = si + dx[1], nj = sj + dy[1]; sd = 1;
                if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
                if (fi[ni][nj] != '.') continue;
                si = ni, sj = nj;
            }
            else if (str[i] == 'U') {
                int ni = si + dx[2], nj = sj + dy[2]; sd = 2;
                if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
                if (fi[ni][nj] != '.') continue;
                si = ni, sj = nj;
            }
            else if (str[i] == 'L') {
                int ni = si + dx[3], nj = sj + dy[3]; sd = 3;
                if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
                if (fi[ni][nj] != '.') continue;
                si = ni, sj = nj;
            }
            else {
                int ni = si, nj = sj;
                while (true) {
                    ni += dx[sd], nj += dy[sd];
                    if (ni < 0 || ni >= n || nj < 0 || nj >= m) break;
                    if (fi[ni][nj] == '*') { fi[ni][nj] = '.'; break; }
                    else if (fi[ni][nj] == '#') { break; }
                }
            }
//            for (int i = 0; i < n; ++i) cout << fi[i] << endl;
//            if (AAA != T-1) cout << endl;
        }
        if (sd == 0) fi[si][sj] = 'v';
        if (sd == 1) fi[si][sj] = '>';
        if (sd == 2) fi[si][sj] = '^';
        if (sd == 3) fi[si][sj] = '<';
        
        for (int i = 0; i < n; ++i) cout << fi[i] << endl;
        if (AAA != T-1) cout << endl;
    }
    return 0;
}
