#include<bits/stdc++.h>
using namespace std;

int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
unordered_map<char,int>U;
int main(){
    U['D']=3;
    U['L']=1;
    U['R']=2;
    int a;cin>>a;
    for(int b=0;b<a;b++){
	if(b)puts("");
	int c,d;cin>>c>>d;
	string f[20];
	int x=0,y=0,z=0;
	for(int e=0;e<c;e++){
	    cin>>e[f];
	    for(int g=0;g<d;g++){
		if(e[f][g]=='^'){x=e,y=g;z=0;e[f][g]='.';}
		else if(e[f][g]=='v'){x=e,y=g;z=3;e[f][g]='.';}
		else if(e[f][g]=='<'){x=e,y=g;z=1;e[f][g]='.';}
		else if(e[f][g]=='>'){x=e,y=g;z=2;e[f][g]='.';}
		}
	    }  
	int n;string S;
	cin>>n>>S;
	for(char i:S){
	    if(i=='S'){
		for(int k=1;x+dx[z]*k>=0&&x+dx[z]*k<c&&y+dy[z]*k>=0&&y+dy[z]*k<d;k++){
		    if(f[x+dx[z]*k][y+dy[z]*k]=='*'){f[x+dx[z]*k][y+dy[z]*k]='.';break;}
		    if(f[x+dx[z]*k][y+dy[z]*k]=='#')break;
		    }
		    continue;
		}
	    z=U[i];
	    if(x+dx[z]>=0&&x+dx[z]<c&&y+dy[z]>=0&&y+dy[z]<d&&f[x+dx[z]][y+dy[z]]=='.'){x+=dx[z];y+=dy[z];}
	    }
	switch(z){
	    case 0:f[x][y]='^';break;
	    case 1:f[x][y]='<';break;
	    case 2:f[x][y]='>';break;
	    case 3:f[x][y]='v';break;
	    }
	 for(int Q=0;Q<c;Q++)cout<<f[Q]<<endl;   
	}
    }