#include <bits/stdc++.h>
using namespace std;

int main(void){
  int n; cin >> n;
  while(n--){
    string order, message;
    cin >> order >> message;
    reverse(order.begin(), order.end());
    for(int pos = 0; pos < order.length(); pos++){
      int len = message.length();
      if(order[pos]  == 'C'){
        char c = message[0];
        for(int i=0; i < len-1; i++){
          message[i] = message[i+1];
        }
        message[len-1] = c;
      }
      if(order[pos]  == 'J'){
        char c = message[len-1];
        for(int i=len-1; i >= 1; i--){
          message[i] = message[i-1];
        }
        message[0] = c;
      }
      if(order[pos]  == 'E'){
        if(len % 2 == 0){
          message = message.substr(len/2, len/2) + message.substr(0, len/2);
        }else{
          message = message.substr(len/2 + 1, len/2) + message.substr(len/2, 1) + message.substr(0, len/2);
        }
      }
      if(order[pos]  == 'A'){
        reverse(message.begin(), message.end());
      }
      if(order[pos]  == 'M'){
        for(int i = 0; i < len; i++){
          if(isdigit(message[i])){
            if(message[i] - '0' == 9) message[i] = '0';
            else{
              message[i] = message[i] - '0' + 1 + '0';
            }
          }
        }
      }
      if(order[pos]  == 'P'){
        for(int i = 0; i < len; i++){
          if(isdigit(message[i])){
            if(message[i] - '0' == 0) message[i] = '9';
            else{
              message[i] = message[i] - '0' - 1 + '0';
            }
          }
        }
      }
    }
    cout << message << endl;
  }
  return 0;
}