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