#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
string revj(string a)
{
  string answer=a+a;
  return string(answer.begin()+a.size()-1,answer.end()-1);
}
string revc(string a)
{
  string answer=a+a;
  return string(answer.begin()+1,answer.begin()+a.size()+1);
}
string reve(string a)
{
  return string(a.end()-a.size()/2,a.end())+string(a.begin()+a.size()/2,a.end()-a.size()/2)+string(a.begin(),a.begin()+a.size()/2);
}
string reva(string a)
{
  return string(a.rbegin(),a.rend());
}
string revp(string a)
{
  string answer=a;
  REP(i,answer.size()) if(0<= answer[i]-'0' && answer[i]-'0' <= 9) answer[i]=(answer[i]-'0'+9)%10+'0';
  return answer;
}
string revm(string a)
{
  string answer=a;
  REP(i,answer.size()) if(0<= answer[i]-'0' && answer[i]-'0' <= 9) answer[i]=(answer[i]-'0'+1)%10+'0';
  return answer;
}
int main(void)
{
  int n;    
  cin >> n;
  REP(i,n){
    string cmd,tmp;
    cin >> cmd >> tmp;
    string rcmd(cmd.rbegin(),cmd.rend());
    for(auto i:rcmd){
      if(i=='J') tmp=revj(tmp);
      if(i=='C') tmp=revc(tmp);
      if(i=='E') tmp=reve(tmp);
      if(i=='A') tmp=reva(tmp);
      if(i=='P') tmp=revp(tmp);
      if(i=='M') tmp=revm(tmp);
    }
    cout << tmp << endl;
  }
  return 0;
}