#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){

  int n;
  cin>>n;
  for(int cnt=0;cnt<n;cnt++){
    string com,s;
    cin>>com>>s;
  reverse(com.begin(),com.end());
  for(int i=0;i<com.size();i++){
    string s0;
    if(com[i]=='C'){
      for(int j=1;j<s.size();j++) s0.push_back(s[j]);
      s0+=s[0];
    }
    else  if(com[i]=='J'){
      s0+=s[s.size()-1];
      for(int j=0;j<s.size()-1;j++) s0.push_back(s[j]);
    }
    else  if(com[i]=='E'){
      int sLen=s.size();
      //cout<<sLen<<endl;
      if(sLen%2) sLen++;
      sLen/=2;
      for(int j=sLen;j<s.size();j++) s0.push_back(s[j]);
      if(s.size()%2) s0.push_back(s[s.size()/2]);
      for(int j=0;j<s.size()/2;j++) s0.push_back(s[j]);
    }
    else  if(com[i]=='A'){
     reverse(s.begin(),s.end());
     s0=s;
    }
    else  if(com[i]=='M'){
      for(int j=0;j<s.size();j++){
	if('0'<=s[j]&&s[j]<='8') s[j]++;
	else if(s[j]=='9') s[j]='0';
      }
      s0=s;
    }
    else  if(com[i]=='P'){
      for(int j=0;j<s.size();j++){
	if('1'<=s[j]&&s[j]<='9') s[j]--;
	else if(s[j]=='0') s[j]='9';
      }
      s0=s;
    }
    s=s0;
    //    cout<<com[i]<<" "<<s<<endl;
  }
  cout<<s<<endl;
  }
  return 0;
  
}