#include <bits/stdc++.h>
using namespace std;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    for (;n--;){
        string order,S; cin >> order >> S;
        int s=S.size();
        reverse(order.begin(),order.end());
        for (int i=0;i<order.size();++i){
            if (order[i]=='J'){
                S+=S;
                S=S.substr(s-1,s);
            }
            if (order[i]=='C'){
                S+=S;
                S=S.substr(1,s);
            }
            if (order[i]=='E'){
                for (int j=0;j<s/2;++j) swap(S[j],S[j+(s+1)/2]);
            }
            if (order[i]=='A'){
                reverse(S.begin(),S.end());
            }
            if (order[i]=='P'){
                for (int j=0;j<s;++j){
                    int x=S[j]-'0';
                    if (x<10) S[j]='0'+(x+9)%10;
                }
            }
            if (order[i]=='M'){
                for (int j=0;j<s;++j){
                    int x=S[j]-'0';
                    if (x<10) S[j]='0'+(x+1)%10;
                }
            }
        }
        cout << S << '\n';
    }
}
