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

string A(string s){
  reverse(s.begin(), s.end());
  return s;
}
string C(string s){
  return s.substr(1, s.length() - 1) + s[0];
}
string J(string s){
  return s.back() + s.substr(0, s.length() - 1);
}
string E(string s){
  string res = s.substr((s.length() + 1)/2);
  if(s.length()%2 != 0)res += s[s.length()/2];
  res += s.substr(0, s.length()/2);
  return res;
}

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

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

int main(){
  int n;
  std::cin >> n;
  for (int roop = 0; roop < n; roop++) {
    string ope, s;
    std::cin >> ope >> s;
    reverse(ope.begin(), ope.end());
    for (int i = 0; i < ope.length(); i++) {
      if(ope[i] == 'A'){
        s = A(s);
      }else if(ope[i] == 'E'){
        s = E(s);
      }else if(ope[i] == 'C'){
        s = C(s);
      }else if(ope[i] == 'J'){
        s = J(s);
      }else if(ope[i] == 'M'){
        s = M(s);
      }else if(ope[i] == 'P'){
        s = P(s);
      }else{
        std::cerr << "hoge" << std::endl;
      }
    }
    std::cout << s << std::endl;

  }
  return 0;
}