using System;
using System.Collections.Generic;

public class Program
{
    /// <summary>
    /// プログラムエントリポイント
    /// </summary>
    public static void Main()
    {
        //問題数入力
        int countQ = int.Parse(Console.ReadLine());

        //問題数分繰り返す
        for (int i = 0; i < countQ; i++)
        {
            //戦車を動かすフィールドの２次元配列の大きさ入力
            string[] inputData = Console.ReadLine().Split(' ');

            //入力されたフィールドの大きさの値をもとに char[][] の配列生成
            char[,] aryChBattleArea = new char[int.Parse(inputData[0]), int.Parse(inputData[1])];

            for (int x = 0; x < aryChBattleArea.GetLength(0); x++)
            {

                char[] tmp = Console.ReadLine().ToCharArray();

                for (int y = 0; y < aryChBattleArea.GetLength(1); y++)
                {
                    aryChBattleArea[x, y] = tmp[y];
                }

            }

            int countOperation = int.Parse(Console.ReadLine());

            char[] aryChOperation = new char[countOperation];

            aryChOperation = Console.ReadLine().ToCharArray();

            BattleTown insBT = new BattleTown(aryChBattleArea, aryChOperation);

            char[,] aryChAnswer = insBT.runOperation();

            for (int x = 0; x < aryChBattleArea.GetLength(0); x++)
            {
                for (int y = 0; y < aryChBattleArea.GetLength(1); y++)
                {
                    Console.Write(aryChAnswer[x, y]);
                }
                Console.WriteLine();
            }
            if (i != countQ - 1)
            {
                Console.WriteLine();
            }

        }
    }
}

/// <summary>
/// BattleTownクラス
/// </summary>
class BattleTown
{

    private char[,] aryChBattleArea;
    private char[] aryChOperation;

    /// <summary>
    /// コンストラクタ
    /// </summary>
    /// <param name="aryChBattleArea">文字２重配列</param>
    /// <param name="aryChOperation">文字配列</param>
    public BattleTown(char[,] aryChBattleArea, char[] aryChOperation)
    {
        this.aryChBattleArea = aryChBattleArea;
        this.aryChOperation = aryChOperation;
    }

    /// <summary>
    /// 命令を実行する
    /// </summary>
    /// <returns></returns>
    public char[,] runOperation()
    {

        foreach (char c in aryChOperation)
        {
            for (int pointX = 0; pointX < aryChBattleArea.GetLength(0); pointX++)
            {
                for (int pointY = 0; pointY < aryChBattleArea.GetLength(1); pointY++)
                {
                    if (aryChBattleArea[pointX, pointY] == '^' || aryChBattleArea[pointX, pointY] == 'v'
                            || aryChBattleArea[pointX, pointY] == '<' || aryChBattleArea[pointX, pointY] == '>')
                    {
                        this.actionTank(pointX, pointY, c);
                        pointX++;
                        break;
                    }

                }

            }
        }

        return this.aryChBattleArea;

    }

    /// <summary>
    /// 戦車を命令に従って動かす
    /// </summary>
    /// <param name="pointX">戦車の位置X</param>
    /// <param name="pointY">戦車の位置Y</param>
    /// <param name="operation">命令(文字)</param>
    private void actionTank(int pointX, int pointY, char operation)
    {
        //命令、向き、動きがリンクしている。
        char[] control = { 'U', 'D', 'R', 'L' };
        char[] direction = { '^', 'v', '>', '<' };
        int[,] move = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };

        //移動用
        if (operation != 'S')
        {
            //control[]の長さ分繰り返す
            for (int i = 0; i < control.Length; i++)
            {
                //命令が一致したら
                if (operation == control[i])
                {
                    //戦車の向きをかえる
                    aryChBattleArea[pointX, pointY] = direction[i];

                    //戦車がX方向,Y方向に動いた先がaryChBattleArea[,]の範囲内だったら

                    //if (pointX - 1 >= 0 && pointX + 1 < aryChBattleArea.Length
                    //        && pointY - 1 >= 0 && pointY + 1 < aryChBattleArea.Length)

                    if (pointX + move[i, 0] >= 0 && pointX + move[i, 0] < aryChBattleArea.GetLength(0)
                            && pointY + move[i, 1] >= 0 && pointY + move[i, 1] < aryChBattleArea.GetLength(1))
                    {
                        //戦車の移動先が '.'(平原) だったら
                        if (aryChBattleArea[pointX + move[i, 0], pointY + move[i, 1]] == '.')
                        {
                            //戦車がいた位置を'.'に変更
                            aryChBattleArea[pointX, pointY] = '.';

                            //戦車の移動先を向きにあわせて変更
                            aryChBattleArea[pointX + move[i, 0], pointY + move[i, 1]] = direction[i];

                        }
                    }
                }
            }

        }

        //射撃用
        else
        {
            int pointXS = pointX;
            int pointXY = pointY;

            for (int i = 0; i < direction.Length; i++)
            {

                if (aryChBattleArea[pointXS, pointXY] == direction[i])
                {

                    while (0 <= pointXS && pointXS < aryChBattleArea.GetLength(0) &&
                                0 <= pointXY && pointXY < aryChBattleArea.GetLength(1))
                    {

                        if (aryChBattleArea[pointXS, pointXY] == '*')
                        {

                            aryChBattleArea[pointXS, pointXY] = '.';
                            break;

                        }
                        else if (aryChBattleArea[pointXS, pointXY] == '#')
                        {

                            break;
                        }

                        pointXS += move[i, 0];
                        pointXY += move[i, 1];
                    }

                    break;
                }
            }
        }
    }
}

Measured memory_bytes: 9220