#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
    string in,s;
    cin>>in>>s;
    for(int i=in.size()-1;i>=0;i--){
      if(in[i]=='C')
	s=s.substr(1)+s[0];
      else if(in[i]=='A')
	reverse(all(s));
      else if(in[i]=='E'){
	//cout<<s.size()<<endl;
	int tmp=s.size();
	if(!(tmp%2))
	  s=s.substr(tmp/2)+s.substr(0,tmp/2);
	else
	  s=s.substr(tmp/2+1)+s[tmp/2]+s.substr(0,tmp/2);
      }else if(in[i]=='J')
	s=s[s.size()-1]+s.substr(0,s.size()-1);
      else if(in[i]=='M'){
	rep(j,s.size())if(isdigit(s[j])){
	  s[j]++;
	  if(s[j]>'9')s[j]='0';
	}
      }else if(in[i]=='P'){
	rep(j,s.size())if(isdigit(s[j])){
	  s[j]--;
	  if(s[j]<'0')s[j]='9';
	}
      }
      //cout<<s<<endl;
    }
    cout<<s<<endl;
  }

}