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

int main(){
  int n;
  cin>>n;
  while(n--){
    string messengers;
    string message;
    cin>>messengers;
    cin>>message;
    for(int i=(int)messengers.size()-1;i>=0;i--){
      if(messengers[i]=='J'){
	char first=message[(int)message.size()-1];
	for(int i=(int)message.size()-1;i>=1;i--){
	  message[i]=message[i-1];
	}
	message[0]=first;
      }else if(messengers[i]=='C'){
	char last=message[0];
	for(int i=0;i<(int)message.size()-1;i++){
	  message[i]=message[i+1];
	}
	message[(int)message.size()-1]=last;
      }else if(messengers[i]=='E'){
	string sleft=message.substr(0,(message.size())/2);
	string sright=message.substr((message.size()+1)/2);
	for(int i=0;i<int(message.size()/2);i++){
	  message[i]=sright[i];
	  message[i+(message.size()+1)/2]=sleft[i];
	}
      }else if(messengers[i]=='A'){
	reverse(message.begin(),message.end());
      }else if(messengers[i]=='P'){
	for(int i=0;i<(int)message.size();i++){
	  if('0'<=message[i] && message[i]<='9'){
	    if(message[i]=='0') message[i]='9';
	    else message[i]--;
	  }
	}
      }else{
	for(int i=0;i<(int)message.size();i++){
	  if('0'<=message[i]&&message[i]<='9'){
	    if(message[i]=='9') message[i]='0';
	    else message[i]++;
	  }
	}
      }
    }
    cout<<message<<endl;
  }
  return 0;
}