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

const char person[] = {'J', 'C', 'E', 'A', 'P', 'M'};

vector<char> decode(const vector<char>& messangers, const vector<char>& message){
  vector<char> ans;

  for(int i=0;i<message.size();++i){
    ans.push_back(message[i]);
  }

  for(int i=messangers.size()-1;i>=0;--i){
    if(messangers[i]==person[0]){
      for(int j=ans.size()-1;j>0;--j){
        swap(ans[j],ans[j-1]);
      }
    }else if(messangers[i]==person[1]){
      for(int j=0;j<ans.size()-1;++j){
        swap(ans[j],ans[j+1]);
      }
    }else if(messangers[i]==person[2]){
      for(int j=0,k;j<ans.size()/2;++j){
        ans.size()%2==1 ? k=ans.size()/2+1 : k=ans.size()/2;
        swap(ans[j],ans[j+k]);
      }
    }else if(messangers[i]==person[3]){
      reverse(ans.begin(), ans.end());
    }else if(messangers[i]==person[4]){
      for(int j=0;j<ans.size();++j){
        if(isdigit(ans[j])){
          if(ans[j]=='0') ans[j]='9';
          else --ans[j];
        }
      }
    }else if(messangers[i]==person[5]){
      for(int j=0;j<ans.size();++j){
        if(isdigit(ans[j])){
          if(ans[j]=='9') ans[j]='0';
          else ++ans[j];
        }
      }
    }
  }

  return ans;
}

int main(){
  int n;
  cin>>n;

  for(int index=0;index<n;++index){
    vector<char> messangers,message;
    string tmp;

    cin>>tmp;
    for(int i=0;i<tmp.size();++i)
      messangers.push_back(tmp.at(i));

    cin>>tmp;
    for(int i=0;i<tmp.size();++i)
      message.push_back(tmp.at(i));

    vector<char> ans;
    ans = decode(messangers, message);

    for(int i=0;i<ans.size();++i)
      cout << ans.at(i);
    
    cout << endl;
  }

  return 0;
}