#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  while (n--) {
    string ord, s;
    cin >> ord >> s;
    reverse(begin(ord), end(ord));
    for (char c : ord) {
      if (c == 'J') {
        rotate(begin(s), prev(end(s)), end(s));
      } else if (c == 'C') {
        rotate(begin(s), next(begin(s)), end(s));
      } else if (c == 'E') {
        if (s.size() & 1) {
          for (int i = 0; i < (int)s.size() / 2; ++i) {
            swap(s[i], s[(s.size() + 1) / 2 + i]);
          }
        } else {
          rotate(begin(s), begin(s) + s.size() / 2, end(s));
        }
      } else if (c == 'A') {
        reverse(begin(s), end(s));
      } else if (c == 'P') {
        for (auto&& e : s) {
          if (isdigit(e)) {
            e = e == '0' ? '9' : e - 1;
          }
        }
      } else {
        assert(c == 'M');
        for (auto&& e : s) {
          if (isdigit(e)) {
            e = e == '9' ? '0' : e + 1;
          }
        }
      }
    }
    cout << s << '\n';
  }
}

