#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int _ = 0; _ < (int)(n); ++_) {
    string order, message;
    cin >> order >> message;
    int l = ceil(message.length() / 2.);
    for (int io = (int)(order.length() - 1); io >= 0; --io) {
      auto o = order[io];
      switch (o) {
        case 'J':
          rotate(message.begin(), message.end() - 1, message.end());
          break;
        case 'C':
          rotate(message.begin(), message.begin() + 1, message.end());
          break;
        case 'E':
          for (int i = 0; i < (int)(message.length() / 2); ++i) {
            swap(message[i], message[i + l]);
          }
          break;
        case 'A':
          reverse((message).begin(), (message).end());
          break;
        case 'P':
          for (int i = 0; i < (int)(message.length()); ++i) {
            if (!isdigit(message[i])) continue;
            message[i] = (message[i] - '0' + 9) % 10 + '0';
          }
          break;
        case 'M':
          for (int i = 0; i < (int)(message.length()); ++i) {
            if (!isdigit(message[i])) continue;
            message[i] = (message[i] - '0' + 1) % 10 + '0';
          }
          break;
      }
    }
    cout << message << endl;
  }
}
