#include <iostream>
#include <algorithm>
#include <ctype.h>
using namespace std;

int main(){
	int n,t;
	string type,s,a,b,c;
	cin >> n;
	while(n--){
		cin >> type >> s;
		reverse(type.begin(),type.end());
		for(int i=0;i<type.length();i++){
			switch(type[i]){
			case 'J':
				rotate(s.begin(),s.begin()+s.length()-1,s.end());
				break;
			case 'C':
				rotate(s.begin(),s.begin()+1,s.end());
				break;
			case 'E':
				t = s.length();
				a = s.substr(0,t/2);
				b = s.substr(t/2+t%2,t/2);
				c = "";if(t%2)c = s[t/2];
				s = b+c+a;
				break;
			case 'A':
				reverse(s.begin(),s.end());
				break;
			case 'P':
				for(int j=0;j<s.length();j++){
					if( isdigit(s[j]) ){
						s[j] = '0'+(s[j]-'0'+9)%10;
					}
				}
				break;
			case 'M':
				for(int j=0;j<s.length();j++){
					if( isdigit(s[j]) ){
						s[j] = '0'+(s[j]-'0'+1)%10;
					}
				}
				break;
			}
		}
		cout << s << endl;
	}
}