import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn._

object Main extends App {

  solve()
  @tailrec def solve(): Unit = {
    val n = readInt()
    if (n == 0) return
    val state = Array.tabulate(n){_ ⇒ readLine.trim.map{c ⇒ Some(c).filterNot(_ == '.')}.toArray}
    val op = readLine.trim
    for (line ← calculate(state, op)) {
      println(line)
    }
    solve()
  }
  def calculate(state: Array[Array[Option[Char]]], op: String): Array[String] = {
    val operation = op.toList match {
      case Operation(o) ⇒ o
      case _ ⇒ ???
    }
    val firstOp = firstState(op)
    val initial = firstOp.foldLeft(state){case (acc, dir) ⇒ tilt(acc, dir)}
    firstOp match {
      case _::Nil ⇒ initial.map(_.map(_.getOrElse('.')).mkString)
      case a::b::_ ⇒
        val firstPos = a + b
        fastRotate (initial, operation.exec (firstPos), firstPos).map (_.map (_.getOrElse ('.') ).mkString)
      case _ ⇒ ???
    }
  }
  def fastRotate[T](state: Array[Array[Option[T]]], times: Long, corner: Corner): Array[Array[Option[T]]] = {
    if (times.abs <= 4) rotate(state, times.toInt, corner)
    else if (times % 4 == 0) {
      val coordinates = state.zipWithIndex.map{case (line, y) ⇒ line.zipWithIndex.map{case (o, x) ⇒ o.map{_ ⇒ Coordinate(x, y)}}}
      if (times > 0) {
        val rotated = new Replacement(rotate(coordinates, 4, corner).zipWithIndex.map{case (line, y) ⇒ line.zipWithIndex.map{case (o, x) ⇒ o.getOrElse(Coordinate(x, y))}})
        (rotated * (times / 4)).replace.let{rep ⇒ state.indices.map{y ⇒ state(y).indices.map{x ⇒ rep(y)(x).let{case Coordinate(x, y) ⇒ state(y)(x)}}.toArray}.toArray}
      }else {
        val rotated = new Replacement(rotate(coordinates, -4, corner).zipWithIndex.map{case (line, y) ⇒ line.zipWithIndex.map{case (o, x) ⇒ o.getOrElse(Coordinate(x, y))}})
        (rotated * (times / -4)).replace.let{rep ⇒ state.indices.map{y ⇒ state(y).indices.map{x ⇒ rep(y)(x).let{case Coordinate(x, y) ⇒ state(y)(x)}}.toArray}.toArray}
      }
    }else fastRotate(rotate(state, (times % 4).toInt, corner), times - times % 4, (corner.toInt + times % 4).asCorner)
  }
  def rotate[T](state: Array[Array[Option[T]]], times: Int, corner: Corner): Array[Array[Option[T]]] = {
    if (times == 0) state
    else {
      corner match {
        case UR ⇒ if (times > 0) rotate(tilt(state, Down), times - 1, DR) else rotate(tilt(state, Left), times + 1, UL)
        case DR ⇒ if (times > 0) rotate(tilt(state, Left), times - 1, DL) else rotate(tilt(state, Up), times + 1, UR)
        case DL ⇒ if (times > 0) rotate(tilt(state, Up), times - 1, UL) else rotate(tilt(state, Right), times + 1, DR)
        case UL ⇒ if (times > 0) rotate(tilt(state, Right), times - 1, UR) else rotate(tilt(state, Down), times + 1, DL)
      }
    }
  }
  def firstState(op: String): List[Direction] = {
    @tailrec def f(o: List[Char]): List[Direction] = {
      o match {
        case d::Nil ⇒ List(Direction(d))
        case 'U'::'R'::_ ⇒ List(Up, Right)
        case 'U'::'L'::_ ⇒ List(Up, Left)
        case 'R'::'U'::_ ⇒ List(Right, Up)
        case 'R'::'D'::_ ⇒ List(Right, Down)
        case 'D'::'R'::_ ⇒ List(Down, Right)
        case 'D'::'L'::_ ⇒ List(Down, Left)
        case 'L'::'U'::_ ⇒ List(Left, Up)
        case 'L'::'D'::_ ⇒ List(Left, Down)
        case _::t ⇒ f(t)
        case _ ⇒ ???
      }
    }
    f(op.toList.filter(_.isUpper))
  }
  def tilt[T](state: Array[Array[Option[T]]], direction: Direction): Array[Array[Option[T]]] = {
    val result = Array.fill(state.length, state.head.length){None: Option[T]}
    direction match {
      case Up ⇒
        for (i ← result.head.indices) {
          var current = 0
          for (j ← state.indices if state(j)(i).isDefined) {
            result(current)(i) = state(j)(i)
            current += 1
          }
        }
        result
      case Down ⇒
        for (i ← result.head.indices) {
          var current = result.indices.last
          for (j ← state.indices.reverse if state(j)(i).isDefined) {
            result(current)(i) = state(j)(i)
            current -= 1
          }
        }
        result
      case Left ⇒
        for (i ← result.indices) {
          var current = 0
          for (j ← state(i).indices if state(i)(j).isDefined) {
            result(i)(current) = state(i)(j)
            current += 1
          }
        }
        result
      case Right ⇒
        for (i ← result.indices) {
          var current = result(i).indices.last
          for (j ← state(i).indices.reverse if state(i)(j).isDefined) {
            result(i)(current) = state(i)(j)
            current -= 1
          }
        }
        result
    }
  }
  case class Coordinate(x: Int, y: Int)
  class Replacement(val replace: Array[Array[Coordinate]]) {
    def +(that: Replacement): Replacement = {
      new Replacement(that.replace.indices.map{y ⇒ that.replace(y).indices.map{ x ⇒ that.replace(y)(x).let{case Coordinate(x, y) ⇒ replace(y)(x)}}.toArray}.toArray)
    }
    def *(that: Long): Replacement = {
      that match {
        case 1 ⇒ this
        case _ if that % 2 == 0 ⇒ (this + this) * (that / 2)
        case _ ⇒ (this + this) * (that / 2) + this
      }
    }
  }
  object Operation {
    def unapply(arg: List[Char]): Option[Operation] = {
      arg match {
        case s Seq Nil ⇒ Some(s)
        case _ ⇒ None
      }
    }
    object AnyOp {
      def unapply(arg: List[Char]): Option[(Operation, List[Char])] = {
        arg match {
          case o SingleOp t ⇒ Some(o, t)
          case r Repeat t ⇒ Some(r, t)
          case _ ⇒ None
        }
      }
    }
    object SingleOp {
      def unapply(arg: List[Char]): Option[(Operation, List[Char])] = {
        arg match {
          case h::t if h.isUpper ⇒ Some(SingleOperation(h), t)
          case _ ⇒ None
        }
      }
    }
    object Number {
      def unapply(arg: List[Char]): Option[(Long, List[Char])] = {
        arg.span(_.isDigit) match {
          case (Nil, _) ⇒ None
          case (n, t) ⇒ Some(n.foldLeft(0L){case (acc, i) ⇒ acc * 10 + i.asDigit}, t)
        }
      }
    }
    object Repeat {
      def unapply(arg: List[Char]): Option[(Operation, List[Char])] = {
        arg match {
          case '('::(o Seq ')'::(n Number t))⇒ Some(o.repeat(n), t)
          case _ ⇒ None
        }
      }
    }
    object Seq {
      def unapply(arg: List[Char]): Option[(Operation, List[Char])] = {
        takeAll(arg) match {
          case Some((l, t)) ⇒ Some(OperationSequence(l), t)
          case None ⇒ None
        }
      }
      @tailrec private[this] def takeAll(arg: List[Char], result: List[Operation] = Nil): Option[(List[Operation], List[Char])] = {
        arg match {
          case o AnyOp t ⇒ takeAll(t, o::result)
          case Nil if result.isEmpty ⇒ None
          case _ ⇒ Some(result.reverse, arg)
        }
      }
    }

  }
  object Direction {
    def apply(char: Char): Direction = {
      char match {
        case 'U' ⇒ Up
        case 'R' ⇒ Right
        case 'D' ⇒ Down
        case 'L' ⇒ Left
        case _ ⇒ ???
      }
    }
  }
  sealed trait Direction {
    def +(that: Direction): Corner = {
      (this, that) match {
        case (Up, Right) ⇒ UR
        case (Up, Left) ⇒ UL
        case (Down, Right) ⇒ DR
        case (Down, Left) ⇒ DL
        case (Right, Up) ⇒ UR
        case (Right, Down) ⇒ DR
        case (Left, Up) ⇒ UL
        case (Left, Down) ⇒ DL
        case _ ⇒ ???
      }
    }
  }
  object Up extends Direction
  object Right extends Direction
  object Down extends Direction
  object Left extends Direction
  sealed trait Corner {
    def toInt: Int = {
      this match {
        case UR ⇒ 0
        case DR ⇒ 1
        case DL ⇒ 2
        case UL ⇒ 3
      }
    }
  }
  object UR extends Corner
  object DR extends Corner
  object DL extends Corner
  object UL extends Corner
  implicit class FromLong(val value: Long) {
    def asCorner: Corner = (value % 4 + 4) % 4 match {
      case 0 ⇒ UR
      case 1 ⇒ DR
      case 2 ⇒ DL
      case 3 ⇒ UL
    }
  }
  sealed trait Operation {
    def exec(corner: Corner): Long
    def repeat(times: Long): Operation = {
      times match {
        case 1 ⇒ this
        case _ if times % 2 == 0 ⇒ (this + this).repeat(times / 2)
        case _ ⇒ (this + this).repeat(times / 2) + this
      }
    }
    def +(that: Operation): Operation = OperationSequence(this::that::Nil)
  }
  object SingleOperation {
    def apply(char: Char): SingleOperation = {
      SingleOperation(Direction(char))
    }
  }
  case class SingleOperation(direction: Direction) extends Operation {
    def exec(corner: Corner): Long = {
      direction match {
        case Up ⇒ corner match {
          case DR ⇒ -1
          case DL ⇒ 1
          case _ ⇒ 0
        }
        case Right ⇒ corner match {
          case DL ⇒ -1
          case UL => 1
          case _ ⇒ 0
        }
        case Left ⇒ corner match {
          case DR ⇒ 1
          case UR ⇒ -1
          case _ ⇒ 0
        }
        case Down ⇒ corner match {
          case UR ⇒ 1
          case UL ⇒ -1
          case _ ⇒ 0
        }
      }
    }
  }
  case class OperationSequence(operations: List[Operation]) extends Operation {
    private val cache = mutable.Map[Corner, Long]()
    override def exec(corner: Corner): Long = {
      cache.getOrElseUpdate(corner, operations.foldLeft(corner.toInt.toLong){case (l, op) ⇒ l + op.exec(l.asCorner)} - corner.toInt)
    }
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    def let[A](@inline func: T ⇒ A): A = func(value)
  }
}
