#include<iostream>
#include<vector>
#include<string>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int main(void){
	int n;
	cin>>n;
	for(int k=0;k<n;k++){
		string s,o;
		cin>>o>>s;
		int t=o.size(),si=s.size();
		for(int i=t-1;i>=0;i--){
			if(o[i]=='J'){
				char temp=s[si-1];
				rep(j,1,si){
					s[si-j]=s[si-j-1];
				}
				s[0]=temp;
			}else if(o[i]=='C'){
				char temp=s[0];
				rep(j,0,si-1){
					s[j]=s[j+1];
				}
				s[si-1]=temp;
			}else if(o[i]=='A'){
				rep(j,0,si/2){
					swap(s[j],s[si-1-j]);
				}
			}else if(o[i]=='E'){
				string b,m,e;
				rep(j,0,si/2){
					b+=s[j];
				}
				int l=0;
				if(si%2==1){
					m=s[si/2];
					l++;
				}
				rep(j,si/2+l,si){
					e+=s[j];
				}
				s=e;
				s+=m;
				s+=b;

			}else if(o[i]=='P'){
				rep(j,0,si){
					if(s[j]>='0'&&s[j]<='9'){
						s[j]--;
						if(s[j]<'0'){
							s[j]='9';
						}
					}
				}
			}else{
					rep(j,0,si){
					if(s[j]>='0'&&s[j]<='9'){
						s[j]++;
						if(s[j]>'9'){
							s[j]='0';
						}
					}
				}
			}
		}
		cout<<s<<endl;

	}
	return 0;
}