import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int x, y, H, W;
static char[][] map;
static char direction;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
H = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
map = new char[H][W];
for(int j = 0; j < H; j++) {
String input = br.readLine();
for(int k = 0; k < W; k++) {
map[j][k] = input.charAt(k);
if (map[j][k] == '^' || map[j][k] == 'v' || map[j][k] == '<' || map[j][k] == '>') {
x = j;
y = k;
direction = map[j][k];
}
}
}
int n = Integer.parseInt(br.readLine());
String command = br.readLine();
for(int j = 0; j < n; j++) {
run(command.charAt(j));
}
}
}
public static void run(char command) {
switch (command) {
case 'U':
direction = '^';
move(-1, 0);
break;
case 'D':
direction = 'v';
move(1, 0);
break;
case 'L':
direction = '<';
move(0, -1);
break;
case 'R':
direction = '>';
move(0, 1);
break;
case 'S':
shoot();
break;
}
}
public static void move(int dx, int dy) {
int nx = x + dx;
int ny = y + dy;
if(nx >= 0 && nx < H && ny >= 0 && ny < W && map[nx][ny] == '.') {
map[x][y] = '.';
x = nx;
y = ny;
}
map[x][y] = direction;
}
public static void shoot() {
int dx = 0, dy = 0;
switch (direction) {
case '^': dx = -1; break;
case 'v': dx = 1; break;
case '<': dy = -1; break;
case '>': dy = 1; break;
}
int nx = x, ny = y;
while (true) {
nx += dx;
ny += dy;
if (nx < 0 || nx >= H || ny < 0 || ny >= W) break;
if (map[nx][ny] == '#') break;
if (map[nx][ny] == '*') {
map[nx][ny] = '.';
break;
}
}
}
}