#include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	int n;
	cin >> n;
	cin.ignore();
	REP(i,n){
		
		string s,c;
		getline(cin,s);
		getline(cin,c);
		
		char a[10]={'0','1','2','3','4','5','6','7','8','9'};
		
		REP(j,s.length()){
			switch(s[s.length()-1-j]){
				case 'C':
				rotate(c.begin(),c.begin()+1,c.begin()+c.length());
				break;
				case 'J':
				rotate(c.begin(),c.begin()+c.length()-1,c.begin()+c.length());
				break;
			  case 'E':{
				string tmp1="";string tmp2="";string tmp3="";
				REP(k,c.length()/2){tmp1+=c[k];}
				REP(k,c.length()/2){tmp3+=c[c.length()-c.length()/2+k];}
				if(c.length()%2==1){tmp2+=c[c.length()/2];}
				c = tmp3+tmp2+tmp1;}
				break;
				case 'A':
				reverse(c.begin(),c.end());
				break;
				case 'M':
				REP(k,c.length()){REP(l,10){if(a[l]==c[k]){c[k]=a[(l+1+10)%10];break;}}}
				break;
				case 'P':
				REP(k,c.length()){REP(l,10){if(a[l]==c[k]){c[k]=a[(l-1+10)%10];break;}}}
				break;
			}
		}
		cout << c << endl;
	}	
		return 0;
}