#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string trans;
    cin >> trans;
    string org;
    cin >> org;
    for (int i = 0; i < trans.size(); i++) {
      if (trans[i] == 'J') {
        string str1 = org.substr(0, 1);
        string str2 = org.substr(1, org.size() - 1);
        org = str2 + str1;
      } else if (trans[i] == 'C') {
        string str1 = org.substr(0, org.size() - 1);
        string str2 = org.substr(org.size() - 1, 1);
        org = str2 + str1;
      } else if (trans[i] == 'E') {
        if (org.size() % 2 == 0) {
          string str1 = org.substr(0, org.size() / 2);
          string str2 =
              org.substr((org.size() / 2), org.size() - (org.size() / 2));
          org = str2 + str1;
        } else {
          string str1 = org.substr(0, org.size() / 2);
          string str2 = org.substr((org.size() / 2), 1);
          string str3 =
              org.substr((org.size() / 2) + 1, org.size() - (org.size() / 2));
          org = str3 + str2 + str1;
        }
      } else if (trans[i] == 'A') {
        reverse(org.begin(), org.end());
      } else if (trans[i] == 'P') {
        for (int j = 0; j < org.size(); j++) {
          if (isdigit(org[j])) {
            int tmp = (org[j] - 48 + 1 + 10) % 10;
            org[j] = (tmp + '0');
          }
        }
      } else if (trans[i] == 'M') {
        for (int j = 0; j < org.size(); j++) {
          if (isdigit(org[j])) {
            int tmp = (org[j] - 48 - 1 + 10) % 10;
            org[j] = (tmp + '0');
          }
        }
      }
    }
    cout << org << endl;
  }
  return 0;
}
