import java.util.HashMap;
import java.util.Scanner;

/**
 * <pre>
 * ??????????????????
 * </pre>
 *
 * @author ?????????????????§???
 * @version 1.8_45
 * @since 1.8_45
 */
public class Main {
	/*
	 * ????????°???????????¨?????????????????????
	 */
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		// ?????????????????????????§?????????°
		int data_set = sc.nextInt();

		// ?????????????????§?????????????????????????????????????????¨
		int sysout_cnt = data_set;

		for (int game_cnt = 0; game_cnt < data_set; game_cnt++) {
			// ?????????????????????????????? ???????????????????????????
			int map_height = sc.nextInt();
			int map_width = sc.nextInt();

			// ???????????????????????£?¨?
			Tank tank = null;

			// ????????¨???????????£?????????????????????
			char[][] game_Map = new char[map_height][map_width];

			for (int mh = 0; mh < map_height; mh++) {
				// ??°??¢????????????
				String tmp_row = sc.next();

				for (int mw = 0; mw < map_width; mw++) {
					// ??°??????????£?
					game_Map[mh][mw] = tmp_row.charAt(mw);

					if ("^v<>".indexOf(game_Map[mh][mw]) > -1) {
						// ????????????????????±????????????????´?
						tank = new Tank(game_Map[mh][mw], new int[] { mh, mw });
						game_Map[mh][mw] = '.';
					}
				}
			}
			// ???????????°
			int cmd_set = sc.nextInt();
			// ?????????????????????
			String cmd_row = sc.next();

			for (int cnt = 0; cnt < cmd_set; cnt++) {
				tank_Action(tank, game_Map, cmd_row.charAt(cnt));
			}

			// ????????????????????????????????°????????¨?¨???????
			last_tankLocate(tank, game_Map);

			// ????????????
			sysout_cnt--;

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

		// ????????????????????????
		sc.close();
	}// ?????????????????????????????????

	/*
	 * ???????????????????????°????????¨?¨???????
	 */
	private static void last_tankLocate(Tank tank, char[][] game_Map) {

		game_Map[tank.getPos()[0]][tank.getPos()[1]] = tank.getDir();

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

	/**
	 * ??????????????£????§????????????????????????????
	 *
	 * @param game_Map
	 *            char???????????????
	 * @param tank
	 *            ??????????????????????????¶
	 * @param cmd
	 *            ??????
	 */
	private static void tank_Action(Tank tank, char[][] game_Map, char cmd) {

		if (cmd != 'S') {
			// ?§????
			only_flatland_Moving(game_Map, tank, cmd);
		} else {
			// ??????
			canon_Shot(game_Map, tank);
		}
	} // tank_Action????????????????????????

	/*
	 * ????????????
	 */
	private static void canon_Shot(char[][] game_Map, Tank tank) {
		// ?????¨??°
		int[] pos = new int[] { tank.getPos()[0], tank.getPos()[1] };
		// ???????????????
		int[] addr = { -1, 1 };

		while (true) {
			if (tank.getDir() == '^') {
				if (pos[0] - 1 >= 0) {
					// [-1,0]
					pos[0] += addr[0];
				} else {
					break;
				}
			} else if (tank.getDir() == 'v') {
				if (pos[0] + 1 < game_Map.length) {
					// [+1,0]
					pos[0] += addr[1];
				} else {
					break;
				}
			} else if (tank.getDir() == '<') {
				if (pos[1] - 1 >= 0) {
					// [0,-1]
					pos[1] += addr[0];
				} else {
					break;
				}
			} else if (tank.getDir() == '>') {
				if (pos[1] + 1 < game_Map[0].length) {
					// [0,+1]
					pos[1] += addr[1];
				} else {
					break;
				}
			}

			switch (game_Map[pos[0]][pos[1]]) {
			case '.':
			case '-':
				continue;

			case '*':
				// ??¬?????¬????£???¨??¶????????£????????¬?????¬????????°?????????
				game_Map[pos[0]][pos[1]] = '.';
			case '#':
			}
			break;
		}
	}// canon_Shot????????????????????????

	/**
	 * ?????°???????§??????????
	 *
	 * @param game_Map
	 *            char???????????????
	 * @param tank
	 *            ??????????????????????????¶
	 * @param cmd
	 *            ??????
	 * @param move
	 *            ?????????????????¨??°
	 */
	private static void only_flatland_Moving(char[][] game_Map, Tank tank, char cmd) {

		// ????????????????????¨?????????????????????
		tank.setDir(tank.getCmd_Map().get(cmd));
		int[] move = new int[] { tank.getPos()[0], tank.getPos()[1] };
		int[] addr = { -1, 1 };

		if (tank.getDir() == '^') {
			if (move[0] - 1 >= 0) {
				move[0] += addr[0];
			}
		} else if (tank.getDir() == 'v') {
			if (move[0] + 1 < game_Map.length) {
				move[0] += addr[1];
			}
		} else if (tank.getDir() == '<') {
			if (move[1] - 1 >= 0) {
				move[1] += addr[0];
			}
		} else if (tank.getDir() == '>') {
			if (move[1] + 1 < game_Map[0].length) {
				move[1] += addr[1];
			}
		}
		// ?????????????§????
		if (game_Map[move[0]][move[1]] == '.') {
			tank.setPos(move);
		}
	}// only_flatland_Moving????????????????????????

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

class Tank {
	/**
	 * ??????????????¨??°
	 */
	private int[] pos;

	/**
	 * ??????
	 */
	private char dir;

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

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

	public char getDir() {
		return dir;
	}

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

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

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

	// ?????????????°???¨
	public HashMap<Character, Character> getCmd_Map() {
		return cmd_Map;
	}
}// Tank?????????????????????

Measured memory_bytes: 36964