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

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

def E(m):
    lenm=len(m)
    h=lenm/2
    if h*2==lenm or h==0:
        temp=m[-h:]+m[:h]
    else:
        temp=m[-h:]+m[h]+m[:h]
    return temp

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

def M(m):
    m=list(m)
    for i in range(len(m)):
        if m[i].isdigit():
            if m[i]=="9":
                m[i]="0"
            else:
                m[i]=str(int(m[i])+1)
    return "".join(m)

def P(m):
    m=list(m)
    for i in range(len(m)):
        if m[i].isdigit():
            if m[i]=="0":
                m[i]="9"
            else:
                m[i]=str(int(m[i])-1)
    return "".join(m)

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