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

public class Main{
	/*
	 * ????????°???????????¨?????????????????????
	 */
	public static void main(String[] args) throws IOException {

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

		while ((tmp_row = br.readLine()) != null && !tmp_row.isEmpty()) {

			String[] tmp_line = tmp_row.split(" ");
			// ??????????????????,???????????????
			int map_height = Integer.parseInt(tmp_line[0]);
			int map_width = Integer.parseInt(tmp_line[1]);

			// ????????¨???????????£?????????????????????
			Tank2[][] game_Map = new Tank2[map_height + 2][map_width + 2];
			// ?????????????????????????????±???????´???´???
			Tank2 tank = null;

			// ??°????????????????£?????????????
			for (int wall = 0; wall < map_height + 2; wall++) {
				Arrays.fill(game_Map[wall], new Tank2('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] = new Tank2(tmp_row.charAt(mw));

					if (game_Map[mh][mw + 1].getMap_info() == '^' || game_Map[mh][mw + 1].getMap_info() == 'v'
							|| game_Map[mh][mw + 1].getMap_info() == '<' || game_Map[mh][mw + 1].getMap_info() == '>') {

						// ????????????????????±????????????????´?
						tank = new Tank2(game_Map[mh][mw + 1].getMap_info(), new int[] { mh, mw + 1 });
						game_Map[mh][mw + 1] = new Tank2('.');
					}
				}
			}
			br.readLine();
			// ?????????????????????
			tmp_row = br.readLine();
			for (char cmd : tmp_row.toCharArray())
				command(tank, game_Map, cmd);

			// ????????????????????????????????±?????¨?¨???????
			last_tankLocate(tank, game_Map);
			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].getMap_info());
				}
				System.out.println();
			}
			if (data_set > 0)
				System.out.println();
		} // while?????????????????????
	}// ?????????????????????????????????

	/**
	 * ??????????????£????§?????????????????????????????????????
	 *
	 * @param game_Map
	 * @param tank
	 *
	 * @param cmd
	 *            ??????
	 */
	private static void command(Tank2 tank, Tank2[][] game_Map, char cmd) {
		// ?§????
		if (cmd != 'S') {
			// ??????????????±?????????
			tank.setDir(tank.getMove_Cmd().get(cmd));
			int[] next = new int[] { tank.getPos()[0], tank.getPos()[1] };
			next[0] += tank.getMove_Dir().get(tank.getDir())[0];
			next[1] += tank.getMove_Dir().get(tank.getDir())[1];
			// ?????°???????§??????????
			if (game_Map[next[0]][next[1]].getMap_info() == '.') {
				tank.setPos(next);
			}
			// ??????
		} else {
			// ???????????????????¨????
			int[] shell = new int[] { tank.getPos()[0], tank.getPos()[1] };
			while (true) {
				// ????????????????????¢
				shell[0] += tank.getMove_Dir().get(tank.getDir())[0];
				shell[1] += tank.getMove_Dir().get(tank.getDir())[1];
				switch (game_Map[shell[0]][shell[1]].getMap_info()) {
				case '.':
				case '-':
					continue;
				// ??¬?????¬????£???¨??¶????????£????????¬?????¬????????°?????????
				case '*':
					game_Map[shell[0]][shell[1]] = new Tank2('.');
				case '#':
					// ??????????£???¨??¶????????£????????????????¶????
				case 'W':
				}
				break;
			}
		}
	}// command????????????????????????

	private static void last_tankLocate(Tank2 tank, Tank2[][] game_Map) {
		// ?????????????????¨??°?????°????????¨?¨???????
		game_Map[tank.getPos()[0]][tank.getPos()[1]] = new Tank2(tank.getDir());

	}// last_tankLocate????????????????????????
}

class Tank2 {

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

	/**
	 * ??°??¢?????±
	 */
	private char map_info;

	public int[] getPos() {
		return pos;
	}

	public void setPos(int[] pos) {
		this.pos = pos;
	}

	public char getDir() {
		return dir;
	}

	public void setDir(char dir) {
		this.dir = dir;
	}

	public char getMap_info() {
		return map_info;
	}

	public void setMap_info(char map_info) {
		this.map_info = map_info;
	}

	/*
	 * ????????????????????????
	 */
	public Tank2(char info) {
		map_info = info;
	}

	public Tank2(char dir, int[] tank_pos) {
		this.dir = dir;
		this.pos = tank_pos;
	}

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

	/*
	 * ?§??????¢???
	 */
	private 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 });
		}
	};

	public HashMap<Character, Character> getMove_Cmd() {
		return move_Cmd;
	}

	// public void setMove_Cmd(HashMap<Character, Character> move_Cmd) {
	// this.move_Cmd = move_Cmd;
	// }

	public HashMap<Character, int[]> getMove_Dir() {
		return move_Dir;
	}

	// public void setMove_Dir(HashMap<Character, int[]> move_Dir) {
	// this.move_Dir = move_Dir;
	// }

}// tank2 ?????????????????????

Measured memory_bytes: 48208