#! /usr/bin/python
# -*- coding: utf-8 -*-

n = int(raw_input())
for i in range(n):
    ger = raw_input()
    mes = raw_input()
    
    for j in range(len(ger)-1, -1, -1):
        if ger[j] == 'J':
            mes = mes[len(mes)-1]+mes[0:len(mes)-1]
        elif ger[j] == 'C':
            mes = mes[1:len(mes)]+mes[0]
        elif ger[j] == 'E':
            if len(mes)%2 == 0:
                mes = mes[len(mes)/2:]+mes[0:len(mes)/2]
            else:
                mes = mes[len(mes)/2+1:]+mes[len(mes)/2]+mes[0:len(mes)/2]
        elif ger[j] == 'A':
            mes = mes[::-1]
        elif ger[j] == 'P':
            for k in range(len(mes)):
                if '1' <= mes[k] <= '9':
                    mes = mes[:k]+chr(ord(mes[k])-1)+mes[k+1:]
                    #mes[k] = chr(ord(mes[k])-1)
                elif mes[k] == '0':
                    mes = mes[:k]+'9'+mes[k+1:]
        elif ger[j] == 'M':
            for k in range(len(mes)):
                if '0' <= mes[k] <= '8':
                    mes = mes[:k]+chr(ord(mes[k])+1)+mes[k+1:]
                    #mes[k] = chr(ord(mes[k])+1)
                elif mes[k] == '9':
                    mes = mes[:k]+'0'+mes[k+1:]
    print mes
    