#include<iostream>
#include<algorithm>

using namespace std;

string C(string s){
  return s.substr(1)+s[0];
}

string J(string s){
  return s[s.length()-1]+s.substr(0,s.length()-1);
}

string E(string s){
  if(s.length()%2 == 0){
    //even
    int half = s.length()/2;
    return s.substr(half)+s.substr(0,half);
  }else{
    //odd
    int half = s.length()/2;
    return s.substr(half+1)+s[half]+s.substr(0,half);
  }
}

string A(string s){
  reverse(s.begin(),s.end());
  return s;
}

string M(string s){
  for(int i = 0; i < s.length(); i++)
    if('0' <= s[i] && s[i] < '9') s[i]++;
    else if(s[i] == '9') s[i] = '0';
  return s;
}

string P(string s){
  for(int i = 0; i < s.length(); i++)
    if('0' < s[i] && s[i] <= '9') s[i]--;
    else if(s[i] == '0') s[i] = '9';
  return s;
}
int main(){

  int n;
  cin >> n;
  while(n--){
    string s,m;
    cin >> s >> m;
    for(int i = s.length()-1; i >= 0; i--){
      if(s[i] == 'J') m = J(m);
      if(s[i] == 'C') m = C(m);
      if(s[i] == 'E') m = E(m);
      if(s[i] == 'A') m = A(m);
      if(s[i] == 'P') m = P(m);
      if(s[i] == 'M') m = M(m);
      //   cout << "m " << m << endl;
    }
    cout << m << endl;
  }
  return 0;
}