#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string s, m;
    cin >> s >> m;
    int len = m.length();
    for (int i = s.length() - 1; i >= 0; i--) {
      if (s[i] == 'J')
        rotate(m.begin(), m.begin() + len - 1, m.end());
      else if (s[i] == 'C')
        rotate(m.begin(), m.begin() + 1, m.end());
      else if (s[i] == 'E') {
        if (len % 2 == 0)
          m = m.substr(len / 2) + m.substr(0, len / 2);
        else
          m = m.substr(len / 2 + 1) + m[len / 2] + m.substr(0, len / 2);
      } else if (s[i] == 'A')
        reverse(m.begin(), m.end());
      else if (s[i] == 'P') {
        for (int j = 0; j < len; j++) {
          if (isdigit(m[j])) m[j] = (m[j] - '0' + 9) % 10 + '0';
        }
      } else if (s[i] == 'M') {
        for (int j = 0; j < len; j++) {
          if (isdigit(m[j])) m[j] = (m[j] - '0' + 1) % 10 + '0';
        }
      }
    }
    cout << "@ " << m << endl;
  }
  return 0;
}
