#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int _ = 0; _ < n; _++) {
    string op, s;
    cin >> op >> s;
    reverse(op.begin(), op.end());
    deque<char> str(s.begin(), s.end());
    for (char c : op) {
      if (c == 'C') {
        char t = str.front();
        str.pop_front();
        str.push_back(t);
      } else if (c == 'J') {
        char t = str.back();
        str.pop_back();
        str.push_front(t);
      } else if (c == 'E') {
        for (int i = 0; i < str.size() / 2; i++) {
          char t = str.front();
          str.pop_front();
          str.push_back(t);
        }
      } else if (c == 'A') {
        reverse(str.begin(), str.end());
      } else if (c == 'M') {
        for (int i = 0; i < str.size(); i++) {
          if (isdigit(str[i])) {
            str[i]++;
            if (str[i] == '9' + 1) {
              str[i] = '0';
            }
          }
        }
      } else if (c == 'P') {
        for (char& d : str) {
          if (isdigit(d)) {
            d--;
            if (d == '0' - 1) {
              d = '9';
            }
          }
        }
      }
    }
    for (char c : str) {
      cout << c;
    }
    cout << endl;
  }
}
