### subroutines

def rotate_l(chs)
  chs.rotate
end

def rotate_r(chs)
  chs.rotate(-1)
end

def swap_h(chs)
  len = chs.length
  hlen = len / 2
  
  if len.even?
    chs[hlen, hlen] + chs[0, hlen]
  else
    chs[hlen + 1, hlen] + chs[hlen, 1] + chs[0, hlen]
  end
end

def reverse(chs)
  chs.reverse
end

def inc_num(chs)
  chs0 = []
  for ch in chs
    if ch >= '0' && ch <= '9'
      chs0 << ((ch.to_i + 1) % 10).to_s
    else
      chs0 << ch
    end
  end
  chs0
end

def dec_num(chs)
  chs0 = []
  for ch in chs
    if ch >= '0' && ch <= '9'
      chs0 << ((ch.to_i + 9) % 10).to_s
    else
      chs0 << ch
    end
  end
  chs0
end

### main

n = gets.to_i

n.times do
  cmds = gets.strip.split('')
  chs = gets.strip.split('')
  #p [cmds, chs]

  for cmd in cmds.reverse
    case cmd
    when 'J'
      chs = rotate_r(chs)
    when 'C'
      chs = rotate_l(chs)
    when 'E'
      chs = swap_h(chs)
    when 'A'
      chs = reverse(chs)
    when 'P'
      chs = dec_num(chs)
    when 'M'
      chs = inc_num(chs)
    end
  end

  puts chs.join('')
end