#include <bits/stdc++.h>
using namespace std;
int main() {
  string number = "0123456789";
  int n;
  cin >> n;
  while (n--) {
    string order, message;
    cin >> order >> message;
    for (int i = order.size() - 1; i >= 0; i--) {
      if (order[i] == 'M') {
        for (int j = 0; j < message.size(); j++) {
          if ('0' <= message[j] && message[j] <= '9') {
            int idx = number.find(message[j]);
            message[j] = number[(idx + 1) % number.size()];
          }
        }
      } else if (order[i] == 'P') {
        for (int j = 0; j < number.size(); j++) {
          if ('0' <= message[j] && message[j] <= '9') {
            int idx = number.find(message[j]);
            message[j] = number[(idx - 1 + number.size()) % number.size()];
          }
        }
      } else if (order[i] == 'A') {
        reverse(message.begin(), message.end());
      } else if (order[i] == 'E') {
        for (int i = 0; i < message.size() / 2; i++) {
          swap(message[i], message[i + (message.size() + 1) / 2]);
        }
      } else if (order[i] == 'C') {
        message = message.substr(1, message.size() - 1) + message[0];
      } else if (order[i] == 'J') {
        message =
            message[message.size() - 1] + message.substr(0, message.size() - 1);
      }
    }
    cout << message << endl;
  }
  return 0;
}
