#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;
int main()
{
	int n,i,j,s;
	string o,m,x,y,z;
	string::iterator b,e;
	cin>>n;
	while(n--)
	{
		cin>>o>>m;
		s=m.size();
		for(i=o.size()-1;i>=0;--i)
		{
			b=m.begin(),e=m.end();
			if(o[i]=='J')rotate(b,e-1,e);
			if(o[i]=='C')rotate(b,b+1,e);
			if(o[i]=='E')
			{
				if(s&1)x.assign(b,b+s/2),y=m[s/2],z.assign(b+s/2+1,e);
				else x.assign(b,b+s/2),y="",z.assign(b+s/2,e);
				m=z+y+x;
			}
			if(o[i]=='A')reverse(b,e);
			if(o[i]=='P')for(j=0;j<s;++j)if(isdigit(m[j]))m[j]=(m[j]-'0'+9)%10+'0';
			if(o[i]=='M')for(j=0;j<s;++j)if(isdigit(m[j]))m[j]=(m[j]-'0'+1)%10+'0';
		}
		cout<<m<<endl;
	}
	return 0;
}