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

string J(string s){
  if(s.length()==0)return s;
  string ret="";
  ret+=s[s.length()-1];
  for(int i=0; i<s.length()-1; i++)ret+=s[i];
  return ret;
}
string C(string s){
  if(s.length()==0)return s;
  string ret="";
  for(int i=1; i<s.length(); i++)ret+=s[i];
  ret+=s[0];
  return ret;
}
string E(string s){
  if(s.length()==0)return s;
  string ret1="", ret2="";
  for(int i=0; i<s.length()/2; i++)ret1+=s[i];
  for(int i=(s.length()+1)/2; i<s.length(); i++)ret2+=s[i];
  if(s.length()%2==1)ret2+=s[s.length()/2];
  return ret2+ret1;
}
string A(string s){
  reverse(s.begin(), s.end());
  return s;
}
string P(string s){
  for(int i=0; i<s.length(); i++){
    if('0'<=s[i]&&s[i]<='9'){
      if(s[i]=='0')s[i]='9';
      else s[i]-=1;
    }
  }
  return s;
}
string M(string s){
  for(int i=0; i<s.length(); i++){
    if('0'<=s[i]&&s[i]<='9'){
      if(s[i]=='9')s[i]='0';
      else s[i]+=1;
    }
  }
  return s;
}

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