#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int n;
  cin >> n;
  int dx[] = {0,1,0,-1};
  int dy[] = {-1,0,1,0};
  while(n--) {
    int h,w;
    cin >> h >> w;
    char ba[w][h];
    int nx, ny, d;
    REP(i,h)
      REP(j,w) {
      cin >> ba[j][i];
      if (ba[j][i]=='^') {
        nx = j;
        ny = i;
        d = 0;
      } else if  (ba[j][i]=='>') {
        nx = j;
        ny = i;
        d = 1;
      } else if  (ba[j][i]=='v') {
        nx = j;
        ny = i;
        d = 2;
      } else if  (ba[j][i]=='<') {
        nx = j;
        ny = i;
        d = 3;
      }
    }
    ba[nx][ny] = '.';
    int m;
    cin >> m;
    REP(i,m) {
      char c;
      cin >> c;
      if (c=='S') {
        int sx=nx, sy=ny;
        while(1) {
          sx+=dx[d];
          sy+=dy[d];

          if (sx<0||sx>=w||sy<0||sy>=h) break;
          if (ba[sx][sy]=='*') {
            ba[sx][sy] = '.';
            break;
          } else if (ba[sx][sy] == '#') break;
        }
      } else {
        if (c=='U') {
          d = 0;
        }else if (c=='D')
          d=2;
        else if (c=='L')
          d = 3;
        else
          d=1;
        int nnx = nx+dx[d];
        int nny = ny+dy[d];
        if (nnx>=0&&nnx<w&&nny>=0&&nny<h && ba[nx+dx[d]][ny+dy[d]] == '.') {
          nx += dx[d];
          ny += dy[d];
        }
      }
    }
    string hoge="^>v<";
    ba[nx][ny] = hoge[d];
    REP(y,h) {
      REP(x,w)
        cout << ba[x][y];
      cout << endl;
    }
    if (n)cout << endl;
  }
}