n = gets.to_i
n.times do
    order = gets.chomp
    s = gets.chomp
    order.reverse.each_char do |c|
        case c
        when ?J
            s = s[-1] + s[0..-2]
        when ?C
            s = s[1..-1] + s[0]
        when ?E
            s = s[-(s.size/2)..-1] + s[s.size/2..s.size-1-s.size/2] + s[0..s.size/2-1]
        when ?A
            s = s.reverse
        when ?P
            s = s.each_char.map{|l| l =~ /\d/ ? (l.to_i-1)%10 : l}.join
        when ?M
            s = s.each_char.map{|l| l =~ /\d/ ? (l.to_i+1)%10 : l}.join
        end
    end
    puts s
end