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