#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;
string num = "0123456789";
int main() {
  int n;
  cin >> n;
  while (n--) {
    string ord;
    cin >> ord;
    string s;
    cin >> s;
    reverse(ord.begin(), ord.end());
    for (int i = (0); i < (((int)(ord).size())); i++) {
      string t = s;
      if (ord[i] == 'J') {
        s = t.back();
        s += t.substr(0, ((int)(t).size()) - 1);
      }
      if (ord[i] == 'C') {
        s = t.substr(1, ((int)(t).size()) - 1);
        s += t.front();
      }
      if (ord[i] == 'E') {
        if (((int)(s).size()) % 2) {
          int c = ((int)(s).size()) / 2;
          s = t.substr(c + 1, ((int)(s).size()) - (c + 1));
          s += t[c];
          s += t.substr(0, c);
        } else {
          int c = ((int)(s).size()) / 2;
          s = t.substr(c, ((int)(s).size()) - c);
          s += t.substr(0, c);
        }
      }
      if (ord[i] == 'A') {
        reverse(s.begin(), s.end());
      }
      if (ord[i] == 'P') {
        for (int j = (0); j < (((int)(s).size())); j++) {
          int idx = num.find(s[j]);
          if (idx != std::string::npos) {
            s[j] = num[(((int)(num).size()) + idx - 1) % ((int)(num).size())];
          }
        }
      }
      if (ord[i] == 'M') {
        for (int j = (0); j < (((int)(s).size())); j++) {
          int idx = num.find(s[j]);
          if (idx != std::string::npos) {
            s[j] = num[(((int)(num).size()) + idx + 1) % ((int)(num).size())];
          }
        }
      }
    }
    cout << s << endl;
  }
  return 0;
}
