#include <bits/stdc++.h>

#define REP(i, n) for(int i = 0; i < (int) (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9

using namespace std;

char alphabet[52];

int main(){
  int n;
  cin >> n;
  REP(counter, n) {
    string messenger; cin >> messenger;
    string message; cin >> message;
    string recon = message;
    REP(i, messenger.size()){
      char target = messenger[messenger.size()-1-i];
      if(target == 'J'){
        REP(j, message.size()) recon[j] = message[(j-1+message.size()) % message.size()];
      }else if(target == 'C'){
        REP(j, message.size()) recon[j] = message[(j+1) % message.size()];
      }else if(target == 'E'){
        REP(j, message.size()/2) swap(recon[j], recon[j+message.size()/2+message.size()%2]);
      }else if(target == 'A'){
        reverse(ALL(recon));
      }else if(target == 'P'){
        REP(j, message.size()){
          if('0' <= message[j] and message[j] <= '9'){
            int digit = message[j] - '0';
            digit = (digit - 1 + 10) % 10;
            recon[j] = digit + '0';
          }
        }
      }else if(target == 'M'){
        REP(j, message.size()){
          if('0' <= message[j] and message[j] <= '9'){
            int digit = message[j] - '0';
            digit = (digit + 1) % 10;
            recon[j] = digit + '0';
          }
        }
      }
      message = recon;
    }
    cout << message << endl;
  }
  return 0;
}