#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main( void )
{
	int T;
	cin >> T;

	for ( int lpc = 0; lpc < T; lpc++ )
	{
		// }bv
		int W, H;
		cin >> H >> W;

		vector <string> map(H);
		for ( int i = 0; i < H; i++ )
			cin >> map[i];

		// ú»
		int x;
		int y;
		int a;
		for ( int i = 0; i < H; i++ )
		{
			for ( int j = 0; j < W; j++ )
			{
				// íÔÌê
				a = (map[i][j]=='^')?1:(map[i][j]=='v')?2:(map[i][j]=='<')?3:(map[i][j]=='>')?4:a;
				switch(map[i][j]) { case '^':case 'v':case '<':case '>': x = j; y = i;map[i][j]='.'; }
			}
		}

		// ì
		int N;
		cin >> N;
		
		string ops;
		cin >> ops;
		istringstream is(ops);
		for ( int i = 0; i < N; i++ )
		{
			char op;
			is >> op;
			if ( op == 'U' )
			{
				a = 1;
				if ( y-1 >= 0 && map[y-1][x] == '.' )
				{
					y--;
				}
			}
			else if ( op == 'D' )
			{
				a = 2;
				if ( y+1 < H && map[y+1][x] == '.' )
					y++;
			}
			else if ( op == 'L' )
			{
				a = 3;
				if ( x-1 >= 0 && map[y][x-1] == '.' )
					x--;
			}
			else if ( op == 'R' )
			{
				a = 4;
				if ( x+1 < W && map[y][x+1] == '.' )
					x++;
			}
			else if ( op == 'S' )
			{
				int nx = x;
				int ny = y;
				while (1)
				{
					nx = ((a==3)?nx-1:(a==4?nx+1:nx));
					ny = ((a==1)?ny-1:(a==2?ny+1:ny));
					if ( nx < 0 || nx >= W || ny < 0 || ny >= H )
						break;

					if ( map[ny][nx] == '#' )
						break;
					if ( map[ny][nx] == '*' )
					{
						map[ny][nx] = '.';
						break;
					}
				}
			}

		}

		map[y][x] = ((a==1)?'^':(a==2)?'v':(a==3)?'<':(a==4)?'>':' ');

		// oÍ
		for ( int i = 0; i < H; i++ )
		{
			for ( int j = 0; j < W; j++ )
			{
				cout << map[i][j];
			}
			cout << endl;
		}

		if ( lpc < T-1 )
			cout << endl;

	} // while
	return 0;
}