#include<cstdio>

using namespace std;

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

int main(){
	int t;	scanf("%d",&t);
	for(int k=0;k<t;k++){
		int w,h;	scanf("%d%d",&h,&w);
		char field[20][21];
		int x,y,dir;
		for(int i=0;i<h;i++){
			scanf("%s",field[i]);
			for(int j=0;j<w;j++){
				if(field[i][j]=='^')	x=j,y=i,dir=1;
				if(field[i][j]=='v')	x=j,y=i,dir=3;
				if(field[i][j]=='<')	x=j,y=i,dir=2;
				if(field[i][j]=='>')	x=j,y=i,dir=0;
			}
		}

		int n;	scanf("%d ",&n);
		while(n--){
			char c=getchar();
			if(c=='U' || c=='D' || c=='L' || c=='R'){
				if(c=='U')	dir=1,field[y][x]='^';
				if(c=='D')	dir=3,field[y][x]='v';
				if(c=='L')	dir=2,field[y][x]='<';
				if(c=='R')	dir=0,field[y][x]='>';
				int xx=x+dx[dir],yy=y+dy[dir];
				if(0<=xx && xx<w && 0<=yy && yy<h && field[yy][xx]=='.'){
					field[yy][xx]=field[y][x];
					field[y][x]='.';
					x=xx,y=yy;
				}
			}
			else{
				int sx=x,sy=y;
				while(0<=sx && sx<w && 0<=sy && sy<h){
					if(field[sy][sx]=='*'){ field[sy][sx]='.'; break; }
					if(field[sy][sx]=='#')	break;
					sx+=dx[dir],sy+=dy[dir];
				}
			}
		}

		for(int i=0;i<h;i++)	puts(field[i]);
		if(k<t-1)	putchar('\n');
	}

	return 0;
}