#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstring>

using namespace std;
int main(){
	int h,w;
	int t;
	cin>>t;
	for(int l = 0; l < t; l++){
		char field[21][21];
		memset(field,0,sizeof(field));
		if(l!=0)
			cout<<endl;
		char angle;
		cin>>h>>w;
		int sy,sx;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin>>field[i][j];
				switch(field[i][j]){
				case '^':
					angle='u';
					sy=i;
					sx=j;
					break;
				case 'v':
					angle='d';
					sy=i;
					sx=j;
					break;
				case '>':
					angle='r';
					sy=i;
					sx=j;
					break;
				case '<':
					angle='l';
					sy=i;
					sx=j;
					break;
				default:
					break;
				}
			}
		}
		int num;
		cin>>num;
		string order;
		cin>>order;
		int cx=sx;
		int cy=sy;
		for(int j = 0; j < order.size(); j++){
			char o=order[j];
			if(o=='U'){
				angle='u';
				int ny=cy-1;
				int nx=cx;
				field[cy][cx]='^';
				if(ny>=0&&nx>=0&&ny<h&&nx<w){
					if(field[ny][nx]=='.'){
						field[cy][cx]='.';
						cy=ny;
						cx=nx;
						field[cy][cx]='^';
					}
				}
			}
			else if(o=='D'){
				angle='d';
				int ny=cy+1;
				int nx=cx;
				field[cy][cx]='v';
				if(ny>=0&&nx>=0&&ny<h&&nx<w){
					if(field[ny][nx]=='.'){
						field[cy][cx]='.';
						cy=ny;
						cx=nx;
						field[cy][cx]='v';
					}
				}
			}
			else if(o=='L'){
				angle='l';
				int ny=cy;
				int nx=cx-1;
				field[cy][cx]='<';
				if(ny>=0&&nx>=0&&ny<h&&nx<w){
					if(field[ny][nx]=='.'){
						field[cy][cx]='.';
						cy=ny;
						cx=nx;
						field[cy][cx]='<';
					}
				}
			}
			else if(o=='R'){
				angle='r';
				int ny=cy;
				int nx=cx+1;
				field[cy][cx]='>';
				if(ny>=0&&nx>=0&&ny<h&&nx<w){
					if(field[ny][nx]=='.'){
						field[cy][cx]='.';
						cy=ny;
						cx=nx;
						field[cy][cx]='>';
					}
				}
			}
			else if(o=='S'){
				if(angle=='r'){
					for(int i = cx+1; i < w; i++){
						if(field[cy][i]=='#')
							break;
						else if(field[cy][i]=='*'){
							field[cy][i]='.';
							break;
						}
					}
				}
				else if(angle=='l'){
					for(int i = cx-1; i >= 0; i--){
						if(field[cy][i]=='#')
							break;
						else if(field[cy][i]=='*'){
							field[cy][i]='.';
							break;
						}
					}
				}
				else if(angle=='u'){
					for(int i = cy-1; i >= 0; i--){
						if(field[i][cx]=='#')
							break;
						else if(field[i][cx]=='*'){
							field[i][cx]='.';
							break;
						}
					}
				}
				else if(angle=='d'){
					for(int i = cy+1; i < h; i++){
						if(field[i][cx]=='#')
							break;
						else if(field[i][cx]=='*'){
							field[i][cx]='.';
							break;
						}
					}
				}
			}
		}
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cout<<field[i][j];
			}
			cout<<endl;
		}
	}

	return 0;
}