#include <bits/stdc++.h>
using namespace std;
string A(string s) {
  reverse(s.begin(), s.end());
  return s;
}
string J(string s) {
  string ss = s.substr(s.length() - 1, 1), sss = s.substr(0, s.length() - 1);
  return ss + sss;
}
string C(string s) {
  string ss = s.substr(0, 1), sss = s.substr(1, s.length() - 1);
  return sss + ss;
}
string E(string s) {
  if (s.length() % 2 == 0) {
    string ss = s.substr(0, s.length() / 2);
    string sss = s.substr(s.length() / 2, s.length() / 2);
    return sss + ss;
  } else {
    string ss = s.substr(0, s.length() / 2);
    string tt = s.substr(s.length(), 1);
    string sss = s.substr(s.length() / 2 + 1, s.length() / 2);
    return sss + tt + ss;
  }
}
string P(string s) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '9' or s[i] == '1' or s[i] == '2' or s[i] == '3' or
        s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or
        s[i] == '8') {
      s[i] = (char)((int)s[i] - 1);
      continue;
    }
    if (s[i] == '0') {
      s[i] = '9';
    }
  }
  return s;
}
string M(string s) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0' or s[i] == '1' or s[i] == '2' or s[i] == '3' or
        s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or
        s[i] == '8') {
      s[i] = (char)((int)s[i] + 1);
      continue;
    }
    if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}
int main() {
  int n;
  cin >> n;
  for (int wq = 0; wq < n; wq++) {
    string t, s;
    cin >> t >> s;
    for (int i = t.length() - 1; i >= 0; i--) {
      if (t[i] == 'J') s = J(s);
      if (t[i] == 'C') s = C(s);
      if (t[i] == 'E') s = E(s);
      if (t[i] == 'A') s = A(s);
      if (t[i] == 'P') s = P(s);
      if (t[i] == 'M') s = M(s);
    }
    cout << s << endl;
  }
}
