#include <stdio.h>
#include <string.h>

int x, y, d;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
char map[24][24];

void chDir(char op){
  if(op == 'U') d = 0;
  else if(op == 'R') d = 1;
  else if(op == 'D') d = 2;
  else d = 3;
}

void goStraight(void){
  int nx, ny;

  nx = x + dir[d][1];
  ny = y + dir[d][0];
  if(map[ny][nx] == '.'){
    x = nx;
    y = ny;
  }
}

void shoot(void){
  int tx = x, ty = y;

  for(; map[ty][tx] != '#' && map[ty][tx] != '*'; tx += dir[d][1], ty += dir[d][0]);

  if(map[ty][tx] == '*'){
    map[ty][tx] = '.';
  }
}

int main(void){
  int i, j;
  int n, h, w, len;
  int flg;
  char op[104];

  scanf("%d",&n);
  while(n--){
    scanf("%d%d",&h,&w);

    memset(map,'#',sizeof(map));
    flg = 0;
    for(i = 1; i <= h; i++){
      scanf("%s",&map[i][1]);
      map[i][w+1] = '#';
      for(j = 1; !flg && j <= w; j++){
	if(map[i][j] == '^'){
	  flg = 1;
	  d = 0;
	}
	else if(map[i][j] == '>'){
	  flg = 1;
	  d = 1;
	}
	else if(map[i][j] == 'v'){
	  flg = 1;
	  d = 2;
	}
	else if(map[i][j] == '<'){
	  flg = 1;
	  d = 3;
	}
	if(flg){
	  map[i][j] = '.';
	  x = j;
	  y = i;
	}
      }
    }
    scanf("%d%s",&len,op);

    for(i = 0; i < len; i++){
      if(op[i] == 'S'){
	shoot();
      }
      else{
	chDir(op[i]);
	goStraight();
      }
    }

    if(d == 0){
      map[y][x] = '^';
    }
    else if(d == 1){
      map[y][x] = '>';
    }
    else if(d == 2){
      map[y][x] = 'v';
    }
    else{
      map[y][x] = '<';
    }

    for(i = 1; i <= h; i++){
      for(j = 1; j <= w; j++){
	putchar(map[i][j]);
      }
      putchar('\n');
    }

    if(n != 0){
      putchar('\n');
    }
  }

  return 0;
}