#include<stdio.h>

char map[22][22];

char ctodir(int dir){
  return dir==0?'^':dir==1?'>':dir==2?'v':'<';
}
int dtoc(char ch){
  return ch=='^'?0:ch=='>'?1:ch=='v'?2:3;
}
int main(){
  int i,j,pc,x,y,h,w,k,clen,dir,dc[4][2]={{-1,0},{0,1},{1,0},{0,-1}},cx,cy;
  char found,cmd[101],c;
  for(i=0;i<22;i++)
    map[0][i]=map[i][0]='\0';
  scanf("%d",&pc);
  
  for(i=0;i<pc;i++){
    scanf("%d %d",&h,&w);
    for(j=0,found=0;j<h;j++){
      scanf("%s",&(map[j+1][1]));
      for(k=0;k<w;k++){
	if(!found&&(map[j+1][k+1]=='<'||map[j+1][k+1]=='>'||map[j+1][k+1]=='^'
		    ||map[j+1][k+1]=='v')){
	  x=k+1;
	  y=j+1;
	  found=1;
	  dir=dtoc(map[j+1][k+1]);
	  break;
	}
      }
    }
    for(j=0;j<w;j++) map[h+1][j+1]='\0';
    for(j=0;j<h;j++) map[w+1][j+1]='\0';
    scanf("%d",&clen);
    scanf("%s",cmd);
    for(j=0;j<clen;j++){
      if(cmd[j]=='S'){
	for(cx=x+dc[dir][1],cy=y+dc[dir][0];
	    map[cy][cx];
	    cx+=dc[dir][1],cy+=dc[dir][0]){
	  if(map[cy][cx]=='#'){
	    break;
	  }else if(map[cy][cx]=='*'){
	    map[cy][cx]='.';
	    break;
	  }
	}
      }else{
	dir=cmd[j]=='U'?0:cmd[j]=='R'?1:cmd[j]=='D'?2:3;
	if(map[y+dc[dir][0]][x+dc[dir][1]]=='.'){
	  map[y+dc[dir][0]][x+dc[dir][1]]=ctodir(dir);
	  map[y][x]='.';
	  y+=dc[dir][0];
	  x+=dc[dir][1];
	}else{
	  map[y][x]=ctodir(dir);
	}
      }
    }
    for(j=0;j<h;j++)
      printf("%s\n",map[j+1]+1);
    printf("\n");
  }
  return 0;
}