#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <functional>

using namespace std;

#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)

int main()
{
	unordered_map<char,function<string(string)>> inv;
	inv['J']=[](string s){
		rotate(s.begin(),s.end()-1,s.end());
		return s;
	};
	inv['C']=[](string s){
		rotate(s.begin(),s.begin()+1,s.end());
		return s;
	};
	inv['E']=[](string s){
		rep(i,s.size()/2) swap(s[i],s[(s.size()+1)/2+i]);
		return s;
	};
	inv['A']=[](string s){
		return string(s.rbegin(),s.rend());
	};
	inv['P']=[](string s){
		char ds[]="9012345678";
		rep(i,s.size()) if(isdigit(s[i])) s[i]=ds[s[i]-'0'];
		return s;
	};
	inv['M']=[](string s){
		char ds[]="1234567890";
		rep(i,s.size()) if(isdigit(s[i])) s[i]=ds[s[i]-'0'];
		return s;
	};
	
	int tc; cin>>tc;
	while(tc--){
		string m,s; cin>>m>>s;
		per(i,m.size()) s=inv[m[i]](s);
		cout<<s<<endl;
	}
	
	return 0;
}