#include<iostream>

using namespace std;
#define MAX 21
char map[MAX][MAX];
enum Dir{UP,RIGHT,LEFT,DOWN};
int w,h;
class Tank{
public:
	int dir;
	int x;
	int y;
};
Tank tank;

int judgeDir(char c){
	if(c=='^')return UP;
	if(c=='v')return DOWN;
	if(c=='>')return RIGHT;
	if(c=='<')return LEFT;
}
char getCharDir(){
	if(tank.dir==UP)return '^';
	if(tank.dir==DOWN)return 'v';
	if(tank.dir==RIGHT)return '>';
	if(tank.dir==LEFT)return '<';
}

void init(){
	for(int i=0;i<21;i++){
		for(int j=0;j<21;j++)map[i][j]='N';
	}
}
void shoot(){
	int dy=0;
	int dx=0;
	if(tank.dir==UP)dy=-1;
	if(tank.dir==DOWN)dy=1;
	if(tank.dir==RIGHT)dx=1;
	if(tank.dir==LEFT)dx=-1;
	int x=tank.x;
	int y=tank.y;
	for(;;){
		if(x+dx<0||x+dx>w||y+dy<0||y+dy>h||map[y+dy][x+dx]=='#')break;
		x+=dx;
		y+=dy;
		if(map[y][x]=='*'){
			map[y][x]='.';
			break;
		}
	}
	return ;
}
void next(char command){
	map[tank.y][tank.x]='.';
	if(command=='U'){
		tank.dir=UP;
		if(tank.y-1>=0 && map[tank.y-1][tank.x]=='.'){
			tank.y=tank.y-1;
		}
	}
	else if(command=='D'){
		tank.dir=DOWN;
		if(tank.y+1<h && map[tank.y+1][tank.x]=='.'){
			tank.y=tank.y+1;
		}
	}
	else if(command=='L'){
		tank.dir=LEFT;
		if(tank.x-1>=0 && map[tank.y][tank.x-1]=='.'){
			tank.x=tank.x-1;
		}
	}
	else if(command=='R'){
		tank.dir=RIGHT;
		if(tank.x+1<w && map[tank.y][tank.x+1]=='.'){
			tank.x=tank.x+1;
		}
	}
	else if(command=='S'){
		shoot();
	}
	map[tank.y][tank.x]='@';
}

int main(){
	int d;
	cin>>d;
	for(int k=0;k<d;k++){
		if(k)cout<<endl;
		//cout<<"----------"<<endl;
		cin>>h>>w;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>map[i][j];
				if(map[i][j]=='^'||map[i][j]=='<'||map[i][j]=='>'||map[i][j]=='v'){
					tank.dir=judgeDir(map[i][j]);
					tank.x=j;
					tank.y=i;
					map[i][j]='.';
				}
			}
		}
		int commandNum;
		cin>>commandNum;
		for(char command;commandNum--;){
			cin>>command;
	//		cout<<"command:"<<command<<endl;
			next(command);
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='@')cout<<getCharDir();
				else cout<<map[i][j];
			}
			cout<<endl;
		}
	}
	return 0;
}