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

string J(string str) {
  string a;
  a+= str[str.size()-1];
  for(int i=0;i<str.size()-1;i++) a += str[i];
  return a;
}

string C(string str){
  string a;
  for(int i=1;i<str.size();i++) a+=str[i];
  a += str[0];
  return a;
}

string E(string str) {
  string a = str;
  for(int i=0;i<str.size()/2;i++) 
    a[i] = str[str.size()-str.size()/2+i],a[str.size()-str.size()/2+i]=str[i];
  return a;
}

string P(string str){
  for(int i=0;i<str.size();i++) 
    if('0' <= str[i] && str[i] <= '9') str[i] = (str[i]-'0'-1+10)%10+'0';
  return str;
}

string M(string str){
  for(int i=0;i<str.size();i++) 
    if('0' <= str[i] && str[i] <= '9') str[i] = (str[i]-'0'+1)%10+'0';
  return str;
} 

int main() {
  int n;
  cin >>n;
  for(int q=0;q<n;q++) {
    string order;
    cin >> order;
    reverse(order.begin(),order.end());
    string str;
    cin >> str;
    for(int i=0;i<order.size();i++){
      if(order[i] == 'J') str = J(str);
      else if(order[i] == 'C') str=C(str);
      else if(order[i] == 'E') str = E(str);
      else if(order[i] == 'A') reverse(str.begin(),str.end());
      else if(order[i] == 'P') str = P(str);
      else if(order[i] == 'M') str = M(str);
    }
    cout << str << endl;
  }

  return 0;
}