#include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;
typedef uint64_t u64;

void revj(std::string& str) {
  std::string temp;
  temp += str[str.size() - 1];
  for(int i = 0; i < (int)str.size() - 1; ++i) {
    temp += str[i];
  }
  str = temp;
}

void revc(std::string& str) {
  std::string temp;
  for(int i = 1; i < (int)str.size(); ++i) {
    temp += str[i];
  }
  temp += str[0];
  str = temp;
}

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

void reva(std::string& str) {
  std::reverse(str.begin(), str.end());
}

void revp(std::string& str) {
  for(int i = 0; i < (int)str.size(); ++i) {
    if( '0' <= str[i] and str[i] <= '9' ) {
      str[i] = (str[i] - '0' + 10 - 1) % 10 + '0';
    }
  }
}

void revm(std::string& str) {
  for(int i = 0; i < (int)str.size(); ++i) {
    if( '0' <= str[i] and str[i] <= '9' ) {
      str[i] = (str[i] - '0' + 1) % 10 + '0';
    }
  }
}

int main() {

  int n;
  std::cin >> n;
  for(int i = 0; i < n; ++i) {
    std::string order;
    std::cin >> order;
    std::string message;
    std::cin >> message;
    for(int j = (int)order.size(); j >= 0; --j) {
      switch( order[j] ) {
      case 'J' :
        revj(message);
        break;
      case 'C' :
        revc(message);
        break;
      case 'E' :
        reve(message);
        break;
      case 'A' :
        reva(message);
        break;
      case 'P' :
        revp(message);
        break;
      case 'M' :
        revm(message);
        break;
      }
    }
    std::cout << message << std::endl;
  }
   
  return 0;
}