import scala.annotation.tailrec
import scala.collection.mutable
object Main extends App {
  import scala.io.StdIn._
  val n = readInt()
  for (_ ← 0 until n) {
    val messenger = readLine.trim
    val message = readLine.trim
    val original =
      messenger.foldRight(new Pure(message): Message) {
        case ('J', m) ⇒ m.rotateLeft
        case ('C', m) ⇒ m.rotateRight
        case ('E', m) ⇒ m.swap
        case ('A', m) ⇒ m.reverse
        case ('P', m) ⇒ m.decrement
        case ('M', m) ⇒ m.increment
        case _ ⇒ ???
      }
    println(original.indices.map(original).mkString(""))
  }
}

sealed trait Message extends ((Int) ⇒ Char) {
  def rotateLeft: Message = new RotateLeft(this)
  def rotateRight: Message = new RotateRight(this)
  def swap: Message = new Swap(this)
  def reverse: Message = new Reverse(this)
  def increment: Message = new Increment(this)
  def decrement: Message = new Decrement(this)
  def apply(index: Int): Char
  def indices: Range = 0 until length
  def length: Int
}
case class Pure(message: String) extends Message {
  override def apply(index: Int): Char = message(index)
  override def length: Int = message.length
}
class RotateLeft(private val message: Message) extends Message {
  override def apply(index: Int): Char = message((index + length - 1) % length)
  override val length: Int = message.length
}
class RotateRight(private val message: Message) extends Message {
  override def apply(index: Int): Char = message((index + 1) % length)
  override val length: Int = message.length
}
class Swap(private val message: Message) extends Message {
  override def apply(index: Int): Char = {
    if ((0 until length / 2).contains(index)) message((length + 1) / 2 + index)
    else if ((((length + 1) / 2 until length)).contains(index)) message(index - (length + 1) / 2)
    else message(index)
  }
  override def length: Int = message.length
}
class Reverse(private val message: Message) extends Message {
  override def apply(index: Int): Char = message(length - 1 - index)
  override def length: Int = message.length
}
class Increment(private val message: Message) extends Message {
  override def apply(index: Int): Char = {
    message(index) match {
      case c if c.isDigit ⇒ ((c.asDigit + 1) % 10 + '0').toChar
      case c ⇒ c
    }
  }
  override def length: Int = message.length
}
class Decrement(private val message: Message) extends Message {
  override def apply(index: Int): Char = {
    message(index) match {
      case c if c.isDigit ⇒ ((c.asDigit + 9) % 10 + '0').toChar
      case c ⇒ c
    }
  }
  override def length: Int = message.length
}


