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[] trans_row = tmp_row.split(" ");
			// ??????????????????,???????????????
			int map_height = Integer.parseInt(trans_row[0]);
			int map_width = Integer.parseInt(trans_row[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("^v<>".indexOf(game_Map[mh][mw + 1].getMap_info()) > -1){
						// ????????????????????±????????????????´?
						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())
				tank_Action(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 tank_Action(Tank2 tank, Tank2[][] game_Map, char cmd) {
		/*
		 * ?§????
		 */
		if (cmd != 'S') {
			// ??????????????±?????????
			tank.setDir(tank.getCmd_Map().get(cmd));
			int[] move = new int[] { tank.getPos()[0], tank.getPos()[1] };
			move[0] += tank.getDir_Map().get(tank.getDir())[0];
			move[1] += tank.getDir_Map().get(tank.getDir())[1];
			// ?????°???????§??????????
			if (game_Map[move[0]][move[1]].getMap_info() == '.') {
				tank.setPos(move);
			}
			/*
			 * ??????
			 */
		} 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????????????????????????

	/**
	 * ?????????????????¨??°?????°????????¨?¨???????
	 */
	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: 47932