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

void Swap(string &s){
  for(int i=0;i<s.size()/2;i++){
    swap(s[i],s[(s.size()+1)/2+i]);
  }
}

void Inc(string &s){
  for(int i=0;i<s.size();i++){
    if('0'<=s[i] && s[i]<='8')s[i]++;
    else if(s[i] == '9')s[i] = '0';
  }
}

void Dec(string &s){
  for(int i=0;i<s.size();i++){
    if('1'<=s[i] && s[i]<='9')s[i]--;
    else if(s[i] == '0')s[i] = '9';
  }
}

int main(){
  int n;
  string r,m;

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> r >> m;
    for(int i=r.size()-1;i>=0;i--){
      if(r[i]=='A')reverse(m.begin(),m.end());
      if(r[i]=='C')m = m.substr(1,m.size()-1) + m[0];
      if(r[i]=='E')Swap(m);
      if(r[i]=='M')Inc(m);
      if(r[i]=='J')m = m[m.size()-1] + m.substr(0,m.size()-1);
      if(r[i]=='P')Dec(m);
    }
    cout << m << endl;
  }
}