#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string q, s;
    cin >> q >> s;
    for (int i = s.size() - 1; 0 <= i; i--) {
      if (q[i] == 'C') s = s.substr(1, s.size() - 1) + s[0];
      if (q[i] == 'J') s = s[s.size() - 1] + s.substr(0, s.size() - 1);
      if (q[i] == 'E')
        for (int j = 0; j < s.size() / 2; j++)
          swap(s[j], s[(s.size() + 1) / 2 + j]);
      if (q[i] == 'A') reverse(s.begin(), s.end());
      if (q[i] == 'P')
        for (int j = 0; j < s.size(); j++)
          if (isdigit(s[j])) s[j] = ((s[j] - '0' + 10) - 1) % 10 + '0';
      if (q[i] == 'M')
        for (int j = 0; j < s.size(); j++)
          if (isdigit(s[j])) s[j] = ((s[j] - '0') + 1) % 10 + '0';
    }
    cout << s << endl;
  }
  return 0;
}
