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