import java.io.IOException;
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) throws NumberFormatException, IOException {
		// ?????????
		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;

			// ????????¨???????????£?????????????????????
			Map[][] game_Map = new Map[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] = new Map(tmp_row.charAt(mw));

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

			// ????????????????????¨???????????????
			Map mp = new Map(map_height, map_width);
			for (int cnt = 0; cnt < cmd_set; cnt++) {
				tank_Action(mp, 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].getMap_info());
				}
				System.out.println();
			}
			// AOJ?????????????§???????????????????????????????????????????
			if (sysout_cnt > 0) {
				System.out.println();
			}
		} // for?????????????????????

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

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

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

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

	/**
	 * ??????????????£????§????????????????????????????
	 *
	 * @param mp
	 *            ??????????????????????????¶
	 * @param game_Map
	 *            ??????????????°??????????????????
	 * @param tank
	 *            ??????????????????????????¶
	 * @param cmd
	 *            ??????
	 */
	private static void tank_Action(Map mp, Tank tank, Map[][] game_Map, char cmd) {
		/*
		 * ?§????
		 */
		if (cmd != 'S') {
			// ????????????????????¨?????????????????????
			tank.setDir(tank.getCmd_Map().get(cmd));
			int[] move = new int[] { tank.getPos()[0], tank.getPos()[1] };
			switch (tank.getDir()) {
			// ('U', '^');
			// ('^', { -1, 0 });
			case '^':
				if (move[0] - 1 >= 0) {
					move[0] += mp.getDir_Map().get(tank.getDir())[0];
					if (game_Map[move[0]][move[1]].getMap_info() == '.') {
						tank.setPos(move);
					}
				}
				break;

			// ('D', 'v');
			// ('v', { 1, 0 });
			case 'v':
				if (move[0] + 1 < mp.getHeight()) {
					move[0] += mp.getDir_Map().get(tank.getDir())[0];
					if (game_Map[move[0]][move[1]].getMap_info() == '.') {
						tank.setPos(move);
					}
				}
				break;
			// ('L', '<');
			// ('<', { 0, -1 });
			case '<':
				if (move[1] - 1 >= 0) {
					move[1] += mp.getDir_Map().get(tank.getDir())[1];
					if (game_Map[move[0]][move[1]].getMap_info() == '.') {
						tank.setPos(move);
					}
				}
				break;
			// ('R', '>');
			// ('>',{ 0, 1 });
			case '>':
				if (move[1] + 1 < mp.getWidth()) {
					move[1] += mp.getDir_Map().get(tank.getDir())[1];
					if (game_Map[move[0]][move[1]].getMap_info() == '.') {
						tank.setPos(move);
					}
				}
				break;
			}
			/*
			 * ??????
			 *
			 * ????????? ?????????????????¨??????????????£???if-else if???????????¨??? ??????????????£???????¬??????????????????§????????????????????????????¨????
			 */
		} else {
			// ???????????????????¨????
			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]].getMap_info() == '*') {
						game_Map[cnt][shell[1]] = new Map('.');
						break;
					} else if (game_Map[cnt][shell[1]].getMap_info() == '#') {
						break;
					}
				}
				break;
			// ('v',{ 1, 0 });
			case 'v':
				for (int cnt = 1; cnt < (mp.getHeight()) - shell[0]; cnt++) {
					if (game_Map[shell[0] + cnt][shell[1]].getMap_info() == '*') {
						game_Map[shell[0] + cnt][shell[1]] = new Map('.');
						break;
					} else if (game_Map[shell[0] + cnt][shell[1]].getMap_info() == '#') {
						break;
					}
				}
				break;
			// ('<', { 0, -1 });
			case '<':
				for (int cnt = shell[1]; cnt >= 0; cnt--) {
					if (game_Map[shell[0]][cnt].getMap_info() == '*') {
						game_Map[shell[0]][cnt] = new Map('.');
						break;
					} else if (game_Map[shell[0]][cnt].getMap_info() == '#') {
						break;
					}
				}
				break;
			// ('>', { 0, 1 });
			case '>':
				for (int cnt = 1; cnt < (mp.getWidth()) - shell[1]; cnt++) {
					if (game_Map[shell[0]][shell[1] + cnt].getMap_info() == '*') {
						game_Map[shell[0]][shell[1] + cnt] = new Map('.');
						break;
					} else if (game_Map[shell[0]][shell[1] + cnt].getMap_info() == '#') {
						break;
					}
				}
				break;
			}
		}
	} // tank_Action????????????????????????
}// ??????????????????????????????

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?????????????????????

class Map {

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

	/**
	 * ??????????????????
	 */
	private int height;

	/**
	 * ??????????????????
	 */
	private int width;

	public char getMap_info() {
		return map_info;
	}

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

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public Map() {

	}

	public Map(char info) {
		map_info = info;
	}

	public Map(int map_height, int map_width) {
		this.height = map_height;
		this.width = map_width;
	}

	/**
	 * ?§??????¢???????????????
	 */
	private HashMap<Character, int[]> dir_Map = 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, int[]> getDir_Map() {
		return dir_Map;
	}

}// Map?????????????????????

Measured memory_bytes: 46480