#include <bits/stdc++.h>
using namespace std;
string A(string in) {
  reverse(in.begin(), in.end());
  return in;
}
string J(string in) {
  in = in.substr(in.size() - 1) + in.substr(0, in.size() - 1);
  return in;
}
string M(string in) {
  for (int i = 0; i < in.size(); i++)
    if ('0' <= in[i] && in[i] <= '9') {
      if (in[i] == '9')
        in[i] = '0';
      else
        in[i]++;
    }
  return in;
}
string P(string in) {
  for (int i = 0; i < in.size(); i++)
    if ('0' <= in[i] && in[i] <= '9') {
      if (in[i] == '0')
        in[i] = '9';
      else
        in[i]--;
    }
  return in;
}
string C(string in) {
  in = in.substr(1) + in.substr(0, 1);
  return in;
}
string E(string in) {
  string res = "";
  for (int i = in.size() / 2; i < in.size(); i++) res += in[i];
  for (int i = 0; i < in.size() / 2; i++) res += in[i];
  return res;
}
int main(void) {
  int N;
  cin >> N;
  while (N--) {
    string a, s;
    cin >> a >> s;
    reverse(a.begin(), a.end());
    for (int i = 0; i < a.size(); i++) {
      switch (a[i]) {
        case 'A':
          s = A(s);
          break;
        case 'J':
          s = J(s);
          break;
        case 'M':
          s = M(s);
          break;
        case 'P':
          s = P(s);
          break;
        case 'C':
          s = C(s);
          break;
        case 'E':
          s = E(s);
          break;
      }
      cout << s << endl;
    }
  }
}
