#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999

int main(){
    int n;
    cin>>n;
    rep(i,n){//JCEAPM
        string member,str;
        cin>>member>>str;
        reverse(all(member));
        rep(i,member.size()){
//            cout<<str<<endl;
            if(member[i] == 'J'){
                str = string(1,str[str.size()-1])+str.substr(0,str.size()-1);
            }else if(member[i] == 'C'){
                str = str.substr(1,str.size()-1)+string(1,str[0]);
            }else if(member[i] == 'E'){
                if(str.size()%2==0){
                    str = str.substr(str.size()/2,str.size()/2)+str.substr(0,str.size()/2);
                }
                else{
                    str = str.substr(str.size()/2+1,str.size()/2)+str[str.size()/2]+str.substr(0,str.size()/2);
                }
            }else if(member[i] == 'A'){
                reverse(all(str));
            }else if(member[i] == 'P'){
                rep(j,str.size()){
                    if(isdigit(str[j])){
                        if(str[j] == '0')str[j] ='9';
                        else str[j]--;
                    }
                }
            }else if(member[i] == 'M'){
                rep(j,str.size()){
                    if(isdigit(str[j])){
                        if(str[j] == '9')str[j] = '0';
                        else str[j]++;
                    }
                }
            }
        }
        cout<<str<<endl;
    }
}