#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int T,H,W,N,dx[]={0,0,-1,1},dy[]={-1,1,0,0};
char f[20][20],c;

int main(){
	cin>>T;
	while(T--){
		cin>>H>>W;
		int x,y,d;
		rep(i,H)rep(j,W){
			cin>>f[i][j];
			if(f[i][j]=='^'){y=i;x=j;d=0;}
			if(f[i][j]=='v'){y=i;x=j;d=1;}
			if(f[i][j]=='<'){y=i;x=j;d=2;}
			if(f[i][j]=='>'){y=i;x=j;d=3;}
		}
		cin>>N;
		rep(i,N){
			cin>>c;
			if(c=='U'){
				d=0;f[y][x]='^';
				if(y>0&&f[y-1][x]=='.'){
					y--;
					f[y][x]='^';
					f[y+1][x]='.';
				}
			}
			if(c=='D'){
				d=1;f[y][x]='v';
				if(y<H-1&&f[y+1][x]=='.'){
					y++;
					f[y][x]='v';
					f[y-1][x]='.';
				}
			}
			if(c=='L'){
				d=2;f[y][x]='<';
				if(x>0&&f[y][x-1]=='.'){
					x--;
					f[y][x]='<';
					f[y][x+1]='.';
				}
			}
			if(c=='R'){
				d=3;f[y][x]='>';
				if(x<W-1&&f[y][x+1]=='.'){
					x++;
					f[y][x]='>';
					f[y][x-1]='.';
				}
			}
			if(c=='S'){
				int bx=x,by=y;
				while(bx>=0&&bx<=W-1&&by>=0&&by<=H-1){
					bx+=dx[d];by+=dy[d];
					if(f[by][bx]=='*'){f[by][bx]='.';break;}
					if(f[by][bx]=='#')break;
				}
			}
		}
		rep(i,H)rep(j,W)cout<<f[i][j]<<(j<W-1?"":"\n");
		if(T)cout<<endl;
	}
}