using System;

public class P
{
    public int x { get; set; }
    public int y { get; set; }
}

public class Hello
{
    public static void Main()
    {
        var p = 0;
        var t = int.Parse(Console.ReadLine().Trim());
        while (p < t)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            var h = int.Parse(line[0]);
            var w = int.Parse(line[1]);
            var map = new char[h, w];
            var now = new P();
            var pre = 0;
            for (int i = 0; i < h; i++)
            {
                var s = Console.ReadLine().Trim();
                for (int j = 0; j < w; j++)
                {
                    var a = s[j];
                    if (a == '>') { map[i, j] = '.'; now.x = i; now.y = j; pre = 0; }
                    else if (a == 'v') { map[i, j] = '.'; now.x = i; now.y = j; pre = 1; }
                    else if (a == '<') { map[i, j] = '.'; now.x = i; now.y = j; pre = 2; }
                    else if (a == '^') { map[i, j] = '.'; now.x = i; now.y = j; pre = 3; }
                    else map[i, j] = a;
                }
            }
            var n = int.Parse(Console.ReadLine().Trim());
            var s2 = Console.ReadLine().Trim();
            for (int i = 0; i < n; i++)
            {
                if (s2[i] == 'U')
                {
                    pre = 3;
                    if (now.x - 1 >= 0)
                    {
                        var a = map[now.x - 1, now.y];
                        if (a == '.') now.x -= 1;
                    }
                }
                else if (s2[i] == 'R')
                {
                    pre = 0;
                    if (now.y + 1 < w)
                    {
                        var a = map[now.x, now.y + 1];
                        if (a == '.') now.y += 1;
                    }
                }
                else if (s2[i] == 'L')
                {
                    pre = 2;
                    if (now.y - 1 >= 0)
                    {
                        var a = map[now.x, now.y - 1];
                        if (a == '.') now.y -= 1;
                    }
                }
                else if (s2[i] == 'D')
                {
                    pre = 1;
                    if (now.x + 1 < h)
                    {
                        var a = map[now.x + 1, now.y];
                        if (a == '.') now.x += 1;
                    }
                }
                else shot(map, pre, h, w, now);
            }
            print(map, now, pre);
            if (p != t - 1) Console.WriteLine();
            p++;
        }
    }

    public static void print(char[,] map, P now, int pre)
    {
        var h = map.GetLength(0);
        var w = map.GetLength(1);
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (now.x == i && now.y == j)
                {
                    if (pre == 0) Console.Write('>');
                    else if (pre == 1) Console.Write('v');
                    else if (pre == 2) Console.Write('<');
                    else Console.Write('^');
                }
                else  Console.Write(map[i,j]);
            }
            Console.WriteLine();
        }
    }
    public static void shot(char[,] map, int pre, int h, int w, P now)
    {
        var dx = new int[] { 0, 1, 0, -1 };
        var dy = new int[] { 1, 0, -1, 0 };
        var nx = now.x;
        var ny = now.y;
        while (true)
        {
            nx += dx[pre];
            ny += dy[pre];
            if (nx < 0 | nx >= h | ny < 0 | ny >= w) return;
            if (map[nx, ny] == '*') { map[nx, ny] = '.'; return; }
            else if (map[nx, ny] == '#') return;
        }
    }
}


Measured memory_bytes: 15848