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

using namespace std;

void Search(vector<string>&, int&, int&);
void GoUp(vector<string>&, int&, int&);
void GoDown(vector<string>&, int&, int&);
void GoLeft(vector<string>&, int&, int&);
void GoRight(vector<string>&, int&, int&);
void ShootUp(vector<string>&, int&, int&);
void ShootDown(vector<string>&, int&, int&);
void ShootLeft(vector<string>&, int&, int&);
void ShootRight(vector<string>&, int&, int&);

int main(){
  int i, j, n, m, h, w, x, y;
  string s;
  vector<string> str;

  cin >> n;

  for(i=0; i<n; ++i){
    cin >> h >> w;

    for(j=0; j<h; ++j){
      cin >> s;
      str.push_back(s);
    }

    Search(str, x, y);
    cin >> m >> s;

    for(j=0; j<s.size(); ++j){
      if(s[j] == 'U') GoUp(str, x, y);
      else if(s[j] == 'D') GoDown(str, x, y);
      else if(s[j] == 'L') GoLeft(str, x, y);
      else if(s[j] == 'R') GoRight(str, x, y);
      else if(s[j] == 'S' && str[x][y] == '^') ShootUp(str, x, y);
      else if(s[j] == 'S' && str[x][y] == 'v') ShootDown(str, x, y);
      else if(s[j] == 'S' && str[x][y] == '<') ShootLeft(str, x, y);
      else if(s[j] == 'S' && str[x][y] == '>') ShootRight(str, x, y);
    }

    if(i != 0) cout << endl;

    for(j=0; j<h; ++j)
      cout << str[j] << endl;

    str.clear();
  }
  return 0;
}

void Search(vector<string>& str, int& x, int& y){
  int i, j;
  for(i=0, x=-1, y=-1; i<str.size(); ++i){
    for(j=0; j<str[i].size(); ++j){
      if(str[i][j] == '^' || str[i][j] == 'v' || str[i][j] == '<' || str[i][j] == '>'){
	x = i;
	y = j;
	break;
      }
    }
    if(x != -1 && y != -1) break;
  }
}

void GoUp(vector<string>& str, int& x, int& y){
  if(x > 0 && str[x-1][y] == '.'){
    str[x-1][y] = '^';
    str[x][y] = '.';
    --x;
  }else{
    str[x][y] = '^';
  }
}

void GoDown(vector<string>& str, int& x, int& y){
  if(x < str.size()-1 && str[x+1][y] == '.'){
    str[x+1][y] = 'v';
    str[x][y] = '.';
    ++x;
  }else{
    str[x][y] = 'v';
  }
}

void GoLeft(vector<string>& str, int& x, int& y){
  if(y > 0 && str[x][y-1] == '.'){
    str[x][y-1] = '<';
    str[x][y] = '.';
    --y;
  }else{
    str[x][y] = '<';
  }
}

void GoRight(vector<string>& str, int& x, int& y){
  if(y < str[x].size()-1 && str[x][y+1] == '.'){
    str[x][y+1] = '>';
    str[x][y] = '.';
    ++y;
  }else{
    str[x][y] = '>';
  }
}

void ShootUp(vector<string>& str, int& x, int& y){
  int i;
  for(i=x-1; i>-1; --i){
    if(str[i][y] == '*'){
      str[i][y] = '.';
      break;
    }else if(str[i][y] == '#'){
      break;
    }
  }
}

void ShootDown(vector<string>& str, int& x, int& y){
  int i;
  for(i=x+1; i<str.size(); ++i){
    if(str[i][y] == '*'){
      str[i][y] = '.';
      break;
    }else if(str[i][y] == '#'){
      break;
    }
  }
}

void ShootLeft(vector<string>& str, int& x, int& y){
  int i;
  for(i=y-1; i>-1; --i){
    if(str[x][i] == '*'){
      str[x][i] = '.';
      break;
    }else if(str[x][i] == '#'){
      break;
    }
  }
}

void ShootRight(vector<string>& str, int& x, int& y){
  int i;
  for(i=y+1; i<str[x].size(); ++i){
    if(str[x][i] == '*'){
      str[x][i] = '.';
      break;
    }else if(str[x][i] == '#'){
      break;
    }
  }
}

Measured memory_bytes: 0