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