n = gets.to_i
n.times do
    o = gets.strip
    m = gets.strip
    o.reverse.each_char do |ord|
        case ord
        when "J"
            m = m.split(//).rotate(-1).join
        when "C"
            m = m.split(//).rotate(1).join
        when "E"
            if m.length % 2 == 0
                m[0...(m.length/2)], m[(m.length/2)...m.length] = m[(m.length/2)...m.length], m[0...(m.length/2)]
            else
                m[0...(m.length/2)], m[(m.length/2 + 1)...m.length] = m[(m.length/2 + 1)...m.length], m[0...(m.length/2)]                
            end
        when "A"
            m.reverse!
        when "P"
            m.each_char.with_index do |c,i|
                if c.match(/[0-9]/)
                    num = (c.to_i - 1)
                    m[i] = num >= 0 ? num.to_s : "9"
                end
            end
        when "M"
            m.each_char.with_index do |c,i|
                if c.match(/[0-9]/)
                    num = (c.to_i + 1)
                    m[i] = num < 10 ? num.to_s : "0"
                end
            end
        end
    end
    puts m
end