#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define PRIME1 99999883
#define PRIME2 99999893
#define EPS 0.00000001
using namespace std;

int H,W;

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){
	scanf("%d %d",&H,&W);

	char table[H][W+1],order[101];

	int current_row = -1,current_col = -1;

	for(int i = 0; i < H; i++){
		scanf("%s",table[i]);
		if(current_row == -1){
			for(int k = 0; table[i][k] != '\0';k++){
				if(table[i][k] == '^'||table[i][k] == 'v' || table[i][k] == '<' || table[i][k] == '>'){
					current_row = i;
					current_col = k;
					break;
				}
			}
		}
	}

	int N;
	scanf("%d",&N);
	scanf("%s",order);

	for(int i = 0; i < N; i++){
		switch(order[i]){
		case 'U':
			if(rangeCheck(current_row-1,current_col) == true && table[current_row-1][current_col] == '.'){ //?§?????????\??????????????°
				table[current_row-1][current_col] = '^';
				table[current_row][current_col] = '.';
				current_row--;
			}else{
				table[current_row][current_col] = '^';
			}
			break;
		case 'D':
			if(rangeCheck(current_row+1,current_col) == true && table[current_row+1][current_col] == '.'){ //?§?????????\??????????????°
				table[current_row+1][current_col] = 'v';
				table[current_row][current_col] = '.';
				current_row++;
			}else{
				table[current_row][current_col] = 'v';
			}
			break;
		case 'L':
			if(rangeCheck(current_row,current_col-1) == true && table[current_row][current_col-1] == '.'){ //?§?????????\??????????????°
				table[current_row][current_col-1] = '<';
				table[current_row][current_col] = '.';
				current_col--;
			}else{
				table[current_row][current_col] = '<';
			}
			break;
		case 'R':
			if(rangeCheck(current_row,current_col+1) == true && table[current_row][current_col+1] == '.'){ //?§?????????\??????????????°
				table[current_row][current_col+1] = '>';
				table[current_row][current_col] = '.';
				current_col++;
			}else{
				table[current_row][current_col] = '>';
			}
			break;
		case 'S':
			switch(table[current_row][current_col]){
			case '^':
				for(int row = current_row-1; row >= 0; row--){
					if(table[row][current_col] == '*'){
						table[row][current_col] = '.';
						break;
					}else if(table[row][current_col] == '#'){
						break;
					}
				}
				break;
			case '<':
				for(int col = current_col-1; col >= 0; col--){
					if(table[current_row][col] == '*'){
						table[current_row][col] = '.';
						break;
					}else if(table[current_row][col] == '#'){
						break;
					}
				}
				break;
			case '>':
				for(int col = current_col+1; col <= W-1; col++){
					if(table[current_row][col] == '*'){
						table[current_row][col] = '.';
						break;
					}else if(table[current_row][col] == '#'){
						break;
					}
				}
				break;
			case 'v':
				for(int row = current_row+1; row <= H-1; row++){
					if(table[row][current_col] == '*'){
						table[row][current_col] = '.';
						break;
					}else if(table[row][current_col] == '#'){
						break;
					}
				}
				break;
			}
			break;
		}
	}

	for(int i = 0; i < H; i++){
		printf("%s\n",table[i]);
	}
}

int main(){

	int T;
	scanf("%d",&T);

	for(int i = 0; i < T; i++){
		if(i != 0){
			printf("\n");
		}
		func();
	}

    return 0;
}

Measured memory_bytes: 3104