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

using namespace std;

int main(void)
{
	int i1,i2,i3;
	int n;
	string order;
	string s;
	char c;
	string temp1,temp2,temp3;


		cin >> n;

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

			reverse(order.begin(), order.end());

			for(i2=0;i2<order.size();i2++){
				if(order[i2]=='J'){
					c = s[s.size()-1];
					s = s.substr(0,s.size()-1);
					s = c + s;	
				}
				else if(order[i2]=='C'){
					c = s[0];
					s = s.substr(1);
					s = s + c;	
				}
				else if(order[i2]=='E'){
					temp2 = "";
					temp3 = s.substr(0,(s.size()/2));
					if(s.size()%2==1){
						temp2 = s.substr((s.size()/2),1);
						temp1 = s.substr((s.size()/2+1));
					}
					else temp1 = s.substr(s.size()/2);

					//cout << temp1 << " " << temp2 << " " << temp3 << endl;
					s = temp1 + temp2 + temp3;

				}
				else if(order[i2]=='A'){
					reverse(s.begin(), s.end());
				}
				else if(order[i2]=='P'){
					for(i3=0;i3<s.size();i3++){
						if(s[i3] >= '1' && s[i3] <= '9'){
							s[i3] = s[i3] - 1;
						}
						else if(s[i3]=='0'){
							s[i3] = '9';
						}
					}

				}
				else if(order[i2]=='M'){
					for(i3=0;i3<s.size();i3++){
						if(s[i3] >= '0' && s[i3] <= '8'){
							s[i3] = s[i3] + 1;
						}
						else if(s[i3]=='9'){
							s[i3] = '0';
						}
					}
				}

			}

			cout << s << endl;
		}

	return 0;

}