import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;

/**
 * <pre>
 * ??????????????????
 * </pre>
 *
 * @author ?????????????????§???
 * @version 1.8_45
 * @since 1.8_45
 */
public class Main{

	public static void main(String[] args) throws IOException {

		// ?????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		// 1?????????????????????????????????????§????????????????????????°
		int data_set = Integer.parseInt(br.readLine());
		// ??\?????????????????????????????????????????£?¨?
		String tmp_row;

		while ((tmp_row = br.readLine()) != null && !tmp_row.isEmpty()) {
			// ??????4 6 ???????????¢??????????????°???????????¢??????????´?????????????????????°
			String[] tmp_line = tmp_row.split(" ");
			// ??????????????????
			int map_height = Integer.parseInt(tmp_line[0]);
			// ???????????????
			int map_width = Integer.parseInt(tmp_line[1]);
			// ????????¨???????????£?????????????????????
			char[][] game_Map = new char[map_height + 2][map_width + 2];
			// ?????????????????????????????±???????´???´???
			Tank tank = null;

			// ??°????????????????£?????????????
			for (int wall = 0; wall < map_height + 2; wall++) {
				Arrays.fill(game_Map[wall], 'W');
			}

			// ????????????????????°??¢?????\????????????
			for (int mh = 1; mh < map_height + 1; mh++) {
				tmp_row = br.readLine();
				for (int mw = 0; mw < map_width; mw++) {
					// ??°??????????£?
					game_Map[mh][mw + 1] = tmp_row.charAt(mw);

					if ("^v<>".indexOf(game_Map[mh][mw + 1]) > -1) {
						// ????????????????????±????????????????´?
						tank = new Tank(game_Map, game_Map[mh][mw + 1], new int[] { mh, mw + 1 });
						game_Map[mh][mw + 1] = '.';
					}
				}
			}
			br.readLine();
			// ?????????????????????
			tmp_row = br.readLine();
			for (char cmd : tmp_row.toCharArray())
				tank.command(cmd);

			// ????????????????????????????????±?????¨?¨???????
			tank.last_tankLocate();
			data_set--;

			// ??°??¢?????????
			for (int ch = 1; ch < map_height + 1; ch++) {
				for (int cw = 1; cw < map_width + 1; cw++) {
					System.out.print(game_Map[ch][cw]);
				}
				System.out.println();
			}
			if (data_set > 0)
				System.out.println();
		} // while?????????????????????
	}// ?????????????????????????????????

}// ???????????????????????????????????????

class Tank {

	/**
	 * ?????????
	 */
	private char[][] map;

	/**
	 * ??????????????¨??°
	 */
	private int[] pos;
	/**
	 * ??????
	 */
	private char dir;

	/*
	 * ????????????????????????
	 */
	Tank(char[][] map_info, char tank_dir, int[] tank_pos) {
		this.dir = tank_dir;
		this.map = map_info;
		this.pos = tank_pos;
	}

	/*
	 * ?§??????????
	 */
	HashMap<Character, Character> move_Cmd = new HashMap<Character, Character>() {
		{
			put('U', '^');
			put('D', 'v');
			put('L', '<');
			put('R', '>');
		}
	};

	/*
	 * ?§??????¢???
	 */
	HashMap<Character, int[]> move_Dir = new HashMap<Character, int[]>() {
		{
			put('^', new int[] { -1, 0 });
			put('v', new int[] { 1, 0 });
			put('<', new int[] { 0, -1 });
			put('>', new int[] { 0, 1 });
		}
	};

	/**
	 * ??????????????£????§?????????????????????????????????????
	 * 
	 * @param cmd
	 *            ??????
	 */
	void command(char cmd) {

		if (cmd != 'S') {
			// ??????????????±?????????
			this.dir = move_Cmd.get(cmd);
			int[] next = new int[] { this.pos[0], this.pos[1] };
			next[0] += move_Dir.get(this.dir)[0];
			next[1] += move_Dir.get(this.dir)[1];
			// ?????°???????§??????????
			if (map[next[0]][next[1]] == '.') {
				this.pos[0] = next[0];
				this.pos[1] = next[1];
			}
		} else {
			// ???????????????????¨????
			int[] shell = new int[] { this.pos[0], this.pos[1] };
			while (true) {
				//????????????????????¢
				shell[0] += move_Dir.get(this.dir)[0];
				shell[1] += move_Dir.get(this.dir)[1];
				switch (map[shell[0]][shell[1]]) {
				case '.':
				case '-':
					continue;
				// ??¬?????¬????£???¨??¶????????£????????¬?????¬????????°?????????
				case '*':
					map[shell[0]][shell[1]] = '.';
				case '#':
					// ??????????£???¨??¶????????£????????????????¶????
				case 'W':
				}
				break;
			}
		}
	}//command????????????????????????
	
	// ?????????????????¨??°?????°????????¨?¨???????
	void last_tankLocate() {
		
		map[this.pos[0]][this.pos[1]] = this.dir;
	}// last_tankLocate????????????????????????

}// Tank?????????????????????

Measured memory_bytes: 33588