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