#include <bits/stdc++.h>
using namespace std;
string revj(string a) {
  string answer = a + a;
  return string(answer.begin() + a.size() - 1, answer.end() - 1);
}
string revc(string a) {
  string answer = a + a;
  return string(answer.begin() + 1, answer.begin() + a.size() + 1);
}
string reve(string a) {
  return string(a.end() - a.size() / 2, a.end()) +
         string(a.begin() + a.size() / 2, a.end() - a.size() / 2) +
         string(a.begin(), a.begin() + a.size() / 2);
}
string reva(string a) { return string(a.rbegin(), a.rend()); }
string revp(string a) {
  string answer = a;
  for (int i = (0); i < (answer.size()); ++i)
    if (0 <= answer[i] - '0' && answer[i] - '0' <= 9)
      answer[i] = (answer[i] - '0' + 9) % 10 + '0';
  return answer;
}
string revm(string a) {
  string answer = a;
  for (int i = (0); i < (answer.size()); ++i)
    if (0 <= answer[i] - '0' && answer[i] - '0' <= 9)
      answer[i] = (answer[i] - '0' + 1) % 10 + '0';
  return answer;
}
int main(void) {
  int n;
  cin >> n;
  for (int i = (0); i < (n); ++i) {
    string cmd, tmp;
    cin >> cmd >> tmp;
    string rcmd(cmd.rbegin(), cmd.rend());
    for (auto i : rcmd) {
      if (i == 'J') tmp = revj(tmp);
      if (i == 'C') tmp = revc(tmp);
      if (i == 'E') tmp = reve(tmp);
      if (i == 'A') tmp = reva(tmp);
      if (i == 'P') tmp = revp(tmp);
      if (i == 'M') tmp = revm(tmp);
    }
    cout << tmp << endl;
  }
  return 0;
}
