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 tmp_cmdRow = sc.next();

			for (int cnt = 0; cnt < cmd_set; cnt++) {
				tank_Action(tank, game_Map, tmp_cmdRow.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????????????????????????

	/**
	 * ????????????
	 *
	 * @param game_Map
	 *            char???????????????
	 */
	private static void canon_Shot(char[][] game_Map, Tank tank) {

//		// ???????????????????¨????
//		int[] shell = new int[] { tank.getPos()[0], tank.getPos()[1] };
//		// ??????????????????????????????
//		switch (tank.getDir()) {
//		// ('^', { -1, 0 });
//		case '^':
//			for (int cnt = shell[0]; cnt >= 0; cnt--) {
//				if (game_Map[cnt][shell[1]] == '*') {
//					game_Map[cnt][shell[1]] = '.';
//					break;
//				} else if (game_Map[cnt][shell[1]] == '#') {
//					break;
//				}
//			}
//			break;
//		// ('v',{ 1, 0 });
//		case 'v':
//			for (int cnt = 1; cnt < game_Map.length - shell[0]; cnt++) {
//				if (game_Map[shell[0] + cnt][shell[1]] == '*') {
//					game_Map[shell[0] + cnt][shell[1]] = '.';
//					break;
//				} else if (game_Map[shell[0] + cnt][shell[1]] == '#') {
//					break;
//				}
//			}
//			break;
//		// ('<', { 0, -1 });
//		case '<':
//			for (int cnt = shell[1]; cnt >= 0; cnt--) {
//				if (game_Map[shell[0]][cnt] == '*') {
//					game_Map[shell[0]][cnt] = '.';
//					break;
//				} else if (game_Map[shell[0]][cnt] == '#') {
//					break;
//				}
//			}
//			break;
//		// ('>', { 0, 1 });
//		case '>':
//			for (int cnt = 1; cnt < game_Map[1].length - shell[1]; cnt++) {
//				if (game_Map[shell[0]][shell[1] + cnt] == '*') {
//					game_Map[shell[0]][shell[1] + cnt] = '.';
//					break;
//				} else if (game_Map[shell[0]][shell[1] + cnt] == '#') {
//					break;
//				}
//			}
//			break;
//		}
//
		int[] pos = new int[] { tank.getPos()[0], tank.getPos()[1] };
		// ???????????????
		int[] addr = { -1, 1 };

		while (true) {
			if (tank.getDir() == '^') {
				if (pos[0] - 1 >= 0) {

					pos[0] += addr[0];
					if (game_Map[pos[0]][pos[1]] == '*') {
						game_Map[pos[0]][pos[1]] = '.';
						break;
					} else if (game_Map[pos[0]][pos[1]] == '#') {
						break;
					}
				} else {
					break;
				}
			} else if (tank.getDir() == 'v') {
				if (pos[0] + 1 < game_Map.length) {

					pos[0] += addr[1];
					if (game_Map[pos[0]][pos[1]] == '*') {
						game_Map[pos[0]][pos[1]] = '.';
						break;
					} else if (game_Map[pos[0]][pos[1]] == '#') {
						break;
					}
				} else {
					break;
				}
			} else if (tank.getDir() == '<') {
				if (pos[1] - 1 >= 0) {

					pos[1] += addr[0];
					if (game_Map[pos[0]][pos[1]] == '*') {
						game_Map[pos[0]][pos[1]] = '.';
						break;
					} else if (game_Map[pos[0]][pos[1]] == '#') {
						break;
					}
				} else {
					break;
				}
			} else if (tank.getDir() == '>') {
				if (pos[1] + 1 < game_Map[0].length) {

					pos[1] += addr[1];
					if (game_Map[pos[0]][pos[1]] == '*') {
						game_Map[pos[0]][pos[1]] = '.';
						break;
					} else if (game_Map[pos[0]][pos[1]] == '#') {
						break;
					}
				} else {
					break;
				}
			}
		}
	}// canon_Shot????????????????????????

	private static void road_MapInfo(char[][] game_Map, int[] pos) {

		if(game_Map[pos[0]][pos[1]] == '*'){
			game_Map[pos[0]][pos[1]] = '.';
		}
	}

	/**
	 * ?????°???????§??????????
	 *
	 * @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: 37164