#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
	int n;
	cin >> n;
	for(int t=0 ; t < n ; t++ ){
		string order, s;
		cin >> order >> s;
		reverse(order.begin(), order.end());
		for(int i=0 ; i < order.size() ; i++ ){
			char ch = order[i];
			
			if( ch == 'J' ){
				rotate( s.begin() , s.begin()+s.size()-1 , s.end() );
			}else if( ch == 'C' ){
				rotate( s.begin() , s.begin()+1 , s.end() );
			}else if( ch == 'E' ){
				string a, b, c;
				for(int k=0 ; k < s.size()/2 ; k++ ){
					a.push_back(s[k]);
				}
				if( s.size() % 2 == 1 ) b.push_back( s[s.size()/2] );
				for(int k = s.size()/2+(s.size()%2?1:0) ; k < s.size() ; k++ ){
					c.push_back(s[k]);
				}
				s = c + b + a;
			}else if( ch == 'A' ){
				reverse(s.begin(), s.end());
			}else if( ch == 'P' ){
				for(int k=0 ; k < s.size() ; k++ ){
					if( '1' <= s[k] && s[k] <= '9' ) s[k] -= 1;
					else if( s[k] == '0' ) s[k] = '9';
				}
			}else if( ch == 'M' ){
				for(int k=0 ; k < s.size() ; k++ ){
					if( '0' <= s[k] && s[k] <= '8' ) s[k] += 1;
					else if( s[k] == '9' ) s[k] = '0';
				}
			}
		}
		cout << s << endl;
	}
}