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