#include <bits/stdc++.h>
using namespace std;
string s;
void f(char c) {
  if (c == 'C') {
    s = s.substr(1, s.size() - 1) + s.substr(0, 1);
  } else if (c == 'J') {
    s = s.substr(s.size() - 1, 1) + s.substr(0, s.size() - 1);
  } else if (c == 'E') {
    int qwe = s.size() / 2;
    if (s.size() % 2)
      s = s.substr(qwe + 1, qwe) + s.substr(qwe, 1) + s.substr(0, qwe);
    else
      s = s.substr(qwe, qwe) + s.substr(0, qwe);
  } else if (c == 'A') {
    reverse((s).begin(), (s).end());
  } else if (c == 'M') {
    int i;
    for (i = 0; i < s.size(); ++i)
      if ('0' <= s[i] && s[i] <= '9') {
        s[i] == '9' ? s[i] == '0' : ++s[i];
      }
  } else if (c == 'P') {
    int i;
    for (i = 0; i < s.size(); ++i)
      if ('0' <= s[i] && s[i] <= '9') {
        s[i] == '0' ? s[i] == '9' : --s[i];
      }
  }
  return;
}
signed main(void) {
  int i, j, k;
  int n;
  scanf("%d", &n);
  for (int testcase = 0; testcase < n; testcase++) {
    string t;
    cin >> t >> s;
    for (i = t.size() - 1; i > -1; --i) f(t[i]);
    cout << s << "\n";
    s.clear();
    t.clear();
  }
  return 0;
}
