#include <cstdio>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

int n;
char a_[11], b_[33];

int main(void) {
  scanf("%d", &n);
  while (n--) {
    scanf("%s%s", a_, b_);
    string a = a_, b = b_;
    for (int i = a.size() - 1; i >= 0; i--) {
      if (a[i] == 'J') rotate(b.rbegin(), b.rbegin() + 1, b.rend());
      if (a[i] == 'C') rotate(b.begin(), b.begin() + 1, b.end());
      if (a[i] == 'E') {
        reverse(b.begin(), b.end());
        reverse(b.begin(), b.begin() + (b.size())/2);
        reverse(b.begin() + (b.size() + 1)/2, b.end());
      }
      if (a[i] == 'A') reverse(b.begin(), b.end());
      if (a[i] == 'P') {
        for (int j = 0; j < int(b.size()); j++) {
          if (isdigit(b[j])) {
            b[j] = b[j] == '0' ? '9' : (b[j] - 1);
          }
        }
      }
      if (a[i] == 'M') {
        for (int j = 0; j < int(b.size()); j++) {
          if (isdigit(b[j])) {
            b[j] = b[j] == '9' ? '0' : (b[j] + 1);
          }
        }
      }
    }
    printf("%s\n", b.c_str());
  }
  return 0;
}

