#include <bits/stdc++.h>
using namespace std;
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
string A(string s) {
  reverse(s.begin(), s.end());
  return s;
}
string J(string s) { return s[s.size() - 1] + s.substr(0, s.size() - 1); }
string M(string s) {
  for (int i = (0); i < (s.size()); i++) {
    if (isdigit(s[i])) {
      s[i] = char((s[i] - '0' + 1 + 26) % 26 + '0');
    }
  }
  return s;
}
string P(string s) {
  for (int i = (0); i < (s.size()); i++) {
    if (isdigit(s[i])) {
      s[i] = char((s[i] - '0' - 1 + 26) % 26 + '0');
    }
  }
  return s;
}
string E(string s) {
  int h = s.size() / 2;
  if (s.size() % 2) return s.substr(h + 1) + s[h] + s.substr(0, h);
  return s.substr(h) + s.substr(0, h);
}
string C(string s) { return s.substr(1, s.size()) + s[0]; }
signed main() {
  int n;
  cin >> n;
  for (int h = (0); h < (n); h++) {
    string o;
    cin >> o;
    string m;
    cin >> m;
    for (int i = (o.size()) - 1; i >= (0); i--) {
      if (o[i] == 'J') m = J(m);
      if (o[i] == 'C') m = C(m);
      if (o[i] == 'E') m = E(m);
      if (o[i] == 'A') m = A(m);
      if (o[i] == 'P') m = P(m);
      if (o[i] == 'M') m = M(m);
    }
    cout << m << endl;
  }
  return 0;
}
