#include<iostream>
#include<cstring>
using namespace std;

string p(string s){
  for(int i=0;i<s.length();i++){
    if('0'<=s[i] && s[i]<='9'){
      s[i]=s[i] -1;
      if(s[i]=='0'-1)s[i]='9';
    }
  }
  return s;

}

string m(string s){
  for(int i=0;i<s.length();i++){
    if('0'<=s[i] && s[i]<='9'){
      s[i]=(s[i] -'0'+1)%10 + '0';
    }
  }
  return s;
}

string a(string s){
  string res=s;
  for(int i=0;i<s.length()/2;i++){
    res[i]=s[s.length()-1-i];
    res[s.length()-1-i]=s[i];
  }
  return res;
}

string e(string s){
  int half=s.length()/2;
  string first,latter;
  first=s.substr(0,half);
  latter=s.substr(s.length()-half,s.length());
  if(s.length()%2==0){
    return latter + first;
  }else{
    return latter + s[half] + first;
  }
}

string j(string s){
  char tmp=s[s.length()-1];
  for(int i=s.length()-1;i>0;i--){
    s[i]=s[i-1];
  }
  s[0]=tmp;
  return s;
}

string c(string s){
  char tmp=s[0];
  for(int i=0;i<s.length()-1;i++){
    s[i]=s[i+1];
  }
  s[s.length()-1]=tmp;
  return s;
}

int main(){
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    string mg,mes;
    cin>>mg>>mes;
    for(int i=mg.length()-1;i>=0;i--){
      if(mg[i]=='A'){
	mes=a(mes);
      }else if(mg[i]=='J'){
	mes=j(mes);
      }else if(mg[i]=='C'){
	mes=c(mes);
      }else if(mg[i]=='E'){
	mes=e(mes);
      }else if(mg[i]=='P'){
	mes=p(mes);
      }else if(mg[i]=='M'){
	mes=m(mes);
      }
    }
    cout << mes << endl;
  }
  return 0;
}