#include <bits/stdc++.h>
using namespace std;
string dec(string str, char c) {
  if (c == 'J') {
    int e = str.size() - 1;
    str = str[e] + str;
    str.pop_back();
  } else if (c == 'C') {
    str = str + str[0];
    str.erase(str.begin());
  } else if (c == 'E') {
    int t = 0;
    while (2 * t < (str.size() - 1)) {
      swap(str[t], str[(str.size() + 1) / 2 + t]);
      t++;
    }
  } else if (c == 'A') {
    reverse(str.begin(), str.end());
  } else if (c == 'P') {
    for (int i = 0; i < str.size(); i++) {
      if (str[i] >= '0' && str[i] <= '9')
        str[i] = ((str[i] - '0') + 9) % 10 + '0';
    }
  } else if (c == 'M') {
    for (int i = 0; i < str.size(); i++) {
      if (str[i] >= '0' && str[i] <= '9')
        str[i] = ((str[i] - '0') + 1) % 10 + '0';
    }
  }
  return str;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    string ord, mes;
    cin >> ord >> mes;
    reverse(ord.begin(), ord.end());
    for (int i = 0; i < ord.size(); i++) {
      mes = dec(mes, ord[i]);
    }
    cout << mes << endl;
  }
}
