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.getCmd_Map().get(cmd));
			int[] next = new int[] { tank.getPos()[0], tank.getPos()[1] };
			next[0] += tank.getDir_Map().get(tank.getDir())[0];
			next[1] += tank.getDir_Map().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.getDir_Map().get(tank.getDir())[0];
				shell[1] += tank.getDir_Map().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????????????????????????

	/**
	 * ?????????????????¨??°?????°????????¨?¨???????
	 *
	 * @param tank
	 *            ??????????????????????????¶
	 * @param game_Map
	 *            ??????????????°??????????????????
	 */
	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[] position) {
		this.pos = position;
	}

	public char getDir() {
		return dir;
	}

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

	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 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;
	}

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

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

Measured memory_bytes: 48540