#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 a,s;
		cin>>a>>s;
		reverse(all(a));
		rep(i,a.size()){
			if(a[i]=='A')
				reverse(all(s));
			else if(a[i]=='J')
				s=s.substr(s.size()-1)+s.substr(0,s.size()-1);
			else if(a[i]=='M'){
				rep(j,s.size())if(isdigit(s[j])){
					s[j]++;
					if(s[j]=='9'+1)s[j]='0';
				}
			}else if(a[i]=='P'){
				rep(j,s.size())if(isdigit(s[j])){
					s[j]--;
					if(s[j]=='0'-1)s[j]='9';
				}
			}else if(a[i]=='C')
				s=s.substr(1)+s.substr(0,1);
			else if(a[i]=='E'){
				string tmp="";
				int t=0;
				if(s.size()%2){t=1;tmp=s[s.size()/2];}
				s=s.substr(s.size()/2+t)+tmp+s.substr(0,s.size()/2);
			}
		}
		cout<<s<<endl;
	}
}