fun main(args:Array<String>):Unit {
    repeat(readLine()!!.toInt()){
        val (h, w) = readLine()!!.split(' ').map(String::toInt)
        val stage = Stage(h, w)
        var tank = Tank()
        repeat(h){ y ->
            readLine()!!.forEachIndexed { x, c ->
                stage[x, y] = when (c) {
                    '*' -> MapObject.Block
                    '#' -> MapObject.Steal
                    '-' -> MapObject.Water
                    '.' -> MapObject.Plane
                    else -> {
                        tank = Tank(Point(x, y), when (c) {
                            '^' -> TankDirection.Up
                            'v' -> TankDirection.Down
                            '<' -> TankDirection.Left
                            else -> TankDirection.Right
                        })
                        MapObject.Plane
                    }
                }
            }
        }
        readLine()
        readLine()!!.toCharArray().forEach {
            if (it == 'S') {
                var point = tank.bullet.find {
                    when (stage[it]) {
                        MapObject.Block -> true
                        MapObject.Steal -> true
                        else -> false
                    }
                }!!
                when (stage[point]) {
                    MapObject.Block -> {
                        stage[point] = MapObject.Plane
                    }
                    else -> {
                    }
                }
            } else {
                tank = when (it) {
                    'U' -> tank.turnedToUp
                    'D' -> tank.turnedToDown
                    'L' -> tank.turnedToLeft
                    else -> tank.turnedToRight
                }
                when (stage[tank.forward]) {
                    MapObject.Plane -> tank = tank.moved()
                    else -> {
                    }
                }
            }
        }
        if (it != 0) println()
        println(stage.mkString(tank))
    }
}

class Point(val x:Int, val y:Int){
    operator fun plus(other:Point):Point = Point(x + other.x, y + other.y)
}
sealed class MapObject(val asChar:Char){
    object Plane:MapObject('.')
    object Block:MapObject('*')
    object Steal:MapObject('#')
    object Water:MapObject('-')
}
sealed class TankDirection(val asChar:Char, val difference:Point){
    object Up:TankDirection('^', Point(0, -1))
    object Down:TankDirection('v', Point(0, 1))
    object Left:TankDirection('<', Point(-1, 0))
    object Right:TankDirection('>', Point(1, 0))
}
class Stage(val height:Int, val width:Int){
    private val mArray:Array<Array<MapObject>> = Array(height + 2){Array(width * 2){MapObject.Steal as MapObject}}
    operator fun set(x:Int, y:Int, value:MapObject):Unit {
        mArray[y + 1][x + 1] = value
    }
    operator fun set(point:Point, value:MapObject):Unit {
        mArray[point.y + 1][point.x + 1] = value
    }
    operator fun get(x:Int, y:Int):MapObject = mArray[y + 1][x + 1]
    operator fun get(point:Point):MapObject = mArray[point.y + 1][point.x + 1]
    fun mkString(tank:Tank):String {
        val str = (1 .. height).map{y -> (1 .. width).map{x -> mArray[y][x].asChar}.toCharArray()}.toTypedArray()
        str[tank.position.y][tank.position.x] = tank.direction.asChar
        return str.joinToString (separator = "\n"){ it.joinToString("") }
    }
}
class Tank(val position:Point = Point(0, 0), val direction: TankDirection = TankDirection.Up){
    val bullet:Iterable<Point>
        get() = BulletLine(position, direction.difference)
    val turnedToUp:Tank
        get() = Tank(position, TankDirection.Up)
    val turnedToDown:Tank
        get() = Tank(position, TankDirection.Down)
    val turnedToLeft:Tank
        get() = Tank(position, TankDirection.Left)
    val turnedToRight:Tank
        get() = Tank(position, TankDirection.Right)
    val forward:Point
        get() = position + direction.difference
    fun moved():Tank = Tank(forward, direction)
    private class BulletLine(private var current:Point, private val diff:Point):Iterator<Point>, Iterable<Point>{
        override fun iterator(): Iterator<Point> = this
        override fun hasNext(): Boolean = true
        override fun next(): Point {
            current += diff
            return current
        }
    }
}
