def C(message):
    return message[1:]+message[:1]

def J(message):
    return message[-1:]+message[:-1]

def E(message):
    lenm=len(message)
    h=lenm/2
    if lenm%2==1:
        temp=message[-h:]+message[h]+message[:h]
    else:
        temp=message[-h:]+message[:h]
    return temp

def A(message):
    return message[::-1]

def M(message):
    s=""
    for i in message:
        if i.isdigit():
            if i=="9":
                s+="0"
            else:
                s+=str(int(i)+1)
        else:
            s+=i
    return s

def P(message):
    s=""
    for i in message:
        if i.isdigit():
            if i=="0":
                s+="9"
            else:
                s+=str(int(i)-1)
        else:
            s+=i
    return s

for i in range(input()):
    messenger=raw_input()
    message=raw_input()
    for m in messenger[::-1]:
        if m=="J":
            message=J(message)
        elif m=="C":
            message=C(message)
        elif m=="E":
            message=E(message)
        elif m=="A":
            message=A(message)
        elif m=="P":
            message=P(message)
        elif m=="M":
            message=M(message)
    print message