//#define DEBUG

int Nsets, Ncodes; int mapW, mapH;
int tankX, tankY, tankD;//the position and the direction of the tank
char map[20][20]; char code;
void init_map(); void print_map(); void step(); void move(int); void shoot();

int main(){
	int i;
	scanf("%d", &Nsets);
	while(Nsets--){
		init_map();
			#ifdef DEBUG
			printf("map print test.\n");
			print_map();
			#endif
		scanf("%d", &Ncodes);
		for(i = 0; i < Ncodes; i++)step( scanf(" %c", &code) );
		print_map();
		if(Nsets)printf("\n");
	}
	return 0;
}

void step(){
	int i;
	char move_symbol[] = "UDLR";
	for(i = 0; move_symbol[i]; i++){
		if(move_symbol[i] == code){
			move(i);
			return;
		}
	}
	shoot();
}
void move(int d){
		#ifdef DEBUG
		printf("move(%d) is called.\n", d);
		#endif
	int moveX[] = {0,0,-1,1}; int moveY[] = {-1,1,0,0};
	int newX = tankX + moveX[d], newY = tankY + moveY[d];
	char tank_symbol[] = "^v<>";
	//is leagal move?
	if( (0 <= newX && newX < mapW) && (0 <= newY && newY < mapH) &&
	map[newX][newY] == '.'){
		//move
		map[tankX][tankY] = '.';
		tankX = newX, tankY = newY;
	}
	tankD = d;
	map[tankX][tankY] = tank_symbol[tankD];
}
void shoot(){
	int moveX[] = {0,0,-1,1}; int moveY[] = {-1,1,0,0};
	int x = tankX + moveX[tankD], y = tankY + moveY[tankD];
	while(0 <= x && x < mapW && 0 <= y && y < mapH){
		if(map[x][y] == '*'){
			map[x][y] = '.';
			break;
		}else if(map[x][y] == '#'){
			break;
		}
		x += moveX[tankD], y += moveY[tankD];
	}
}

void init_map(){
	int i, j, k;
	char tank_symbol[] = "^v<>";
	scanf("%d %d", &mapH, &mapW);
	for(j = 0; j < mapH; j++)for(i = 0; i < mapW; i++){
		scanf(" %c", &map[i][j]);
		for(k = 0; tank_symbol[k]; k++)if(map[i][j] == tank_symbol[k])tankX = i, tankY = j, tankD = k;
	}
}
void print_map(){
	int i, j;
	for(j = 0; j < mapH; j++){
		for(i = 0; i < mapW; i++)printf("%c", map[i][j]);
		printf("\n");
	}
}