#include<string>
#include<algorithm>
#include<iostream>
using namespace std;

string J(string s, int len) {
  return s.substr(1) + s[0];
}

string C(string s, int len) {
  return s[len-1] + s.substr(0,len-1);
}

string E(string s, int len) {
  if (len % 2 == 1) {
    return s.substr(len/2+1) + s[len/2] + s.substr(0, len/2);
  } else {
    return s.substr(len/2) + s.substr(0, len/2);
  }
}

string A(string s, int len) {
  reverse(s.begin(), s.end());
  return s;
}

string P(string s, int len) {
  for (int i = 0; i < len; i++) {
    if (!isdigit(s[i])) continue;
    if (s[i] == '9') s[i] = '0';
    else s[i]++;
  }
  return s;
}

string M(string s, int len) {
  for (int i = 0; i < len; i++) {
    if (!isdigit(s[i])) continue;
    if (s[i] == '0') s[i] = '9';
    else s[i]--;
  }
  return s;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string messengers, crypt;
    cin >> messengers >> crypt;
    int len = crypt.length();
    reverse(messengers.begin(), messengers.end());
    for (char m : messengers) {
      if (m == 'J') {
        crypt = C(crypt, len);
      } else if (m == 'C') {
        crypt = J(crypt, len);
      } else if (m == 'E') {
        crypt = E(crypt, len);
      } else if (m == 'A') {
        crypt = A(crypt, len);
      } else if (m == 'P') {
        crypt = M(crypt, len);
      } else if (m == 'M') {
        crypt = P(crypt, len);
      }
    }
    cout << crypt << endl;
  }
}