gets.to_i.times{
    order = gets.chomp.split("").reverse
    str = gets.chomp
    order.each{|messenger|
        case messenger
        when "J"
            str = str[-1] + str[0..-2]
        when "C"
            str = str[1..-1] + str[0]
        when "E"
            if str.length > 1
                l = str.length / 2
                str = str[(-l)..-1] + str[0..(l-1)] if str.length.even?
                str = str[(-l)..-1] + str[-l-1] + str[0..(l-1)] if str.length.odd?
            end
        when "A"
            str = str.reverse
        when "P"
            0.upto(str.length - 1){|i| str[i] = ((str[i].to_i - 1) % 10).to_s if ["0","1","2","3","4","5","6","7","8","9"].include? str[i]}
        when "M"
            0.upto(str.length - 1){|i| str[i] = ((str[i].to_i + 1) % 10).to_s if ["0","1","2","3","4","5","6","7","8","9"].include? str[i]}
        end
    }
    puts str
}
