// Aizu 2103: Battle Town
// 2017.9.20 bal4u@uu

#include <stdio.h>

char map[22][22]; int h, w;
char s[102];
char tbl[128];
int mv[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
char dir[4] = { '^', '>', 'v', '<' };

int main()
{
	int cno, t, n, r, c, tr, tc, d, k;
	char *p;

	tbl['^'] = 1, tbl['>'] = 2, tbl['v'] = 3, tbl['<'] = 4;
	tbl['U'] = 1, tbl['R'] = 2, tbl['D'] = 3, tbl['L'] = 4;
	scanf("%d", &t); cno = 0; while (t-- > 0) {
		scanf("%d%d", &h, &w);
		for (r = 0; r < h; r++) {
			scanf("%s", map[r]);
			for (c = 0; c < w; c++) if ((k = tbl[map[r][c]]) > 0) tr = r, tc = c, d = k-1;
		}
		scanf("%d", &n);
		scanf("%s", s); p = s;
		while (*p) {
			if (*p != 'S') {
				d = tbl[*p]-1; r = tr + mv[d][0], c = tc + mv[d][1]; map[tr][tc] = '.';
				if (r >= 0 && r < h && c >= 0 && c < w && map[r][c] == '.') tr = r, tc = c;
				map[tr][tc] = dir[d];
			} else {
				r = tr, c = tc; while (1) {
					r += mv[d][0], c += mv[d][1];
					if (r < 0 || r >= h || c < 0 || c >= w) break;
					if (map[r][c] == '*') { map[r][c] = '.'; break; }
					else if (map[r][c] == '#') break;
				}
			}
			p++;
		}
		if (cno++) putchar('\n');
		for (r = 0; r < h; r++) puts(map[r]);
	}
	return 0;
}