#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string decC(string s)
{
	string mess=s;
	mess[s.size()-1]=s[0];
	for(int i = 0; i < s.size()-1; ++i)
	{
		mess[i]=s[i+1];
	}
	return mess;
}

string decJ(string s)
{
	string mess=s;
	mess[0]=s[s.size()-1];
	for(int i = 1; i < s.size(); ++i)
	{
		mess[i]=s[i-1];
	}
	return mess;
}

string decE(string s)
{
	int n=s.size();
	string l=s.substr(0,n/2);
	string m=n%2==0?"":s.substr(n/2,1);
	string r=n%2==0?s.substr(n/2):s.substr(n/2+1);

	return r+m+l;
}

string decP(string s)
{
	for(int i = 0; i < s.size(); ++i)
	{
		if('0'<=s[i]&&s[i]<='9')
		{
			if(s[i]=='0') s[i]='9';
			else s[i]-=1;
		}
	}
	return s;
}

string decM(string s)
{
	for(int i = 0; i < s.size(); ++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 order,mess; cin>>order>>mess;

		for(int i = order.size()-1; 0 <= i; --i)
		{
			if(order[i]=='J') mess=decJ(mess);
			if(order[i]=='C') mess=decC(mess);
			if(order[i]=='E') mess=decE(mess);
			if(order[i]=='A') reverse(mess.begin(),mess.end());
			if(order[i]=='P') mess=decP(mess);
			if(order[i]=='M') mess=decM(mess);
		}
		cout<<mess<<endl;
	}
  return 0;
}