#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX/3

#define MAX_N 1000

int n;

string f(string s,bool flag){
	string res="";
	rep(i,s.length()){
		if(isdigit(s[i])){
			char c=s[i];
			if(flag)c++;
			else c--;
			if(c>'9')c='0';
			if(c<'0')c='9';
			res+=c;
		}else res+=s[i];
	}
	return res;
}

int main(){
	cin.sync_with_stdio(false);
	cin>>n;
	rep(h,n){
		string s,op;
		cin>>op>>s;
		reverse(all(op));
		int l=s.length();
		string res;
		rep(i,op.length()){
			if(op[i]=='C'){
				s=s.substr(1,l-1)+s[0];
			}else if(op[i]=='J'){
				s=s[l-1]+s.substr(0,l-1);
			}else if(op[i]=='E'){
				if(l%2==1)s=s.substr(l/2+1,l/2)+s[l/2]+s.substr(0,l/2);
				else s=s.substr(l/2,l/2)+s.substr(0,l/2);
			}else if(op[i]=='A'){
				reverse(all(s));
			}else if(op[i]=='P'){
				s=f(s,false);
			}else if(op[i]=='M'){
				s=f(s,true);
			}
		}
		cout<<s<<endl;
	}
	return 0;
}