#include <stdio.h>
#include <string.h>
#include <ctype.h>

void rotate(char* s, int d)
{
    int i, n=strlen(s);
    if (d>0) {
        char t=s[n-1];
        for(i=n-1;i>0;--i) s[i]=s[i-1];
        s[0]=t;
    }
    else if (d<0) {
        char t=s[0];
        for(i=0;i<n-1;++i) s[i]=s[i+1];
        s[n-1]=t;
    }
}

void swap(char* s)
{
    char t[32];
    int n=strlen(s);
    strncpy(t, s, n/2);
    strncpy(s, s+(n+1)/2, n/2);
    strncpy(s+(n+1)/2, t, n/2);
}

void reverse(char* s)
{
    int i, n=strlen(s);
    for(i=0;i<n/2;++i) {
        char t=s[i];
        s[i]=s[n-1-i];
        s[n-1-i]=t;
    }
}

void increment(char* s, int d)
{
    int i, n=strlen(s);
    for(i=0;i<n;++i) if (isdigit(s[i]))
        s[i]=(s[i]-'0'+d+10)%10+'0';
}

int main(void)
{
    int n;
    scanf("%d", &n);
    while (n--) {
        char order[32], message[32];
        int i;
        scanf("%s%s", order, message);
        reverse(order);
        for(i=0;order[i];++i) {
            switch (order[i]) {
                case 'J': rotate(message, 1); break;
                case 'C': rotate(message, -1); break;
                case 'E': swap(message); break;
                case 'A': reverse(message); break;
                case 'P': increment(message, -1); break;
                case 'M': increment(message, 1); break;
            }
        }
        puts(message);
    }
    return 0;
}