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