#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
	string order;
	string mess;

	int n;
	int sz;

	cin>>n;

	for(int i1=0;i1<n;i1++){
		cin>>order;
		cin>>mess;

		reverse(order.begin(), order.end());
		sz=mess.size();

		for(int i2=0;i2<order.size();i2++){
			if(order[i2]=='J'){
				mess=mess[sz-1]+mess.substr(0,sz-1);
			}
			else if(order[i2]=='C'){
				mess=mess.substr(1,sz-1)+mess[0];
			}
			else if(order[i2]=='E'){
				string l,r;
				l=mess.substr(0,sz/2);
				
				if(sz%2==0){	
					r=mess.substr(sz/2,sz/2);
					mess=r+l;
				}
				else {
					r=mess.substr(sz/2+1,sz/2);
					mess=r+mess[sz/2]+l;
				}
			}
			else if(order[i2]=='A'){
				reverse(mess.begin(), mess.end());
			}
			else if(order[i2]=='P'){
				for(int i3=0;i3<sz;i3++){
					if(mess[i3]>='1' && mess[i3]<='9') mess[i3]--;
					else if(mess[i3]=='0') mess[i3]='9';
				}
			}
			else {// M
				for(int i3=0;i3<sz;i3++){
					if(mess[i3]>='0' && mess[i3]<='8') mess[i3]++;
					else if(mess[i3]=='9') mess[i3]='0';
				}
			}
		}

		cout<<mess<<endl;

	}

	return 0;
}