// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;



int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	int n;
	cin>>n;
	while(n--){
		string s,t;
		cin>>s>>t;
		for(int i=sz(s)-1;i>=0;i--){
			if(s[i]=='C'){
				t = t.substr(1) + t.substr(0,1);
			}
			if(s[i]=='J'){
				t = t.substr(sz(t)-1) + t.substr(0,sz(t)-1);
			}
			if(s[i]=='E'){
				if(sz(t)%2==0){
					t = t.substr(sz(t)/2) + t.substr(0,sz(t)/2);
				}else{
					t = t.substr(sz(t)/2+1) + t.substr(sz(t)/2,1) + t.substr(0,sz(t)/2);
				}
			}
			if(s[i]=='A'){
				reverse(t.begin(),t.end());
			}
			if(s[i]=='M'){
				rep(j,sz(t)){
					if(t[j]=='9')t[j]='0';
					else if(isdigit(t[j]))t[j]++;
				}
			}
			if(s[i]=='P'){
				rep(j,sz(t)){
					if(t[j]=='0')t[j]='9';
					else if(isdigit(t[j]))t[j]--;
				}
			}
			
		}
		cout<<t<<endl;
	}
}