#include <bits/stdc++.h>
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string in, s;
    cin >> in >> s;
    for (int i = 0; i < in.size(); i++) {
      if (in[i] == 'J')
        s = s.substr(1) + s[0];
      else if (in[i] == 'A')
        reverse(s.begin(), s.end());
      else if (in[i] == 'E') {
        int tmp = s.size();
        if (!(tmp % 2))
          s = s.substr(tmp / 2) + s.substr(0, tmp / 2);
        else
          s = s.substr(tmp / 2 + 1) + s[tmp / 2] + s.substr(0, tmp / 2);
      } else if (in[i] == 'C')
        s = s[s.size() - 1] + s.substr(0, s.size() - 1);
      else if (in[i] == 'M') {
        for (int j = 0; j < s.size(); j++)
          if (isdigit(s[j])) {
            s[j]++;
            if (s[j] > '9') s[j] = '0';
          }
      } else if (in[i] == 'P') {
        for (int j = 0; j < s.size(); j++)
          if (isdigit(s[j])) {
            s[j]--;
            if (s[j] < '0') s[j] = '9';
          }
      }
    }
    cout << s << endl;
  }
}
