#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;
int main(){
  int n;
  cin >> n;
  while(n--){
    string done,message;
    cin >> done >> message;
    reverse(done.begin(),done.end());
    for(int i = 0 ; i < done.size() ; i++ ){
      switch(done[i]){
      case 'J':
        rotate(message.begin(),message.begin()+message.size()-1,message.end());
        break;
      case 'C':
        rotate(message.begin(),message.begin()+1,message.end());
        break;
      case 'E':
        for(int j = 0 ; j < message.size() / 2 ; j++ ){
          swap(message[j],message[(message.size()+1)/2+j]);
        }
        break;
      case 'A':
        reverse(message.begin(),message.end());
        break;
      case 'P':
        for(int j = 0 ; j < message.size() ; j++ ){
          if(isdigit(message[j])) message[j] = (message[j]-'0'+9)%10+'0';
        }
        break;
      case 'M':
        for(int j = 0 ; j < message.size() ; j++ ){
          if(isdigit(message[j])) message[j] = (message[j]-'0'+1)%10+'0';
        }
        break;
      }
      //cout << message << endl;
    }
    cout << message << endl;
  }
}