#include <bits/stdc++.h>
using namespace std;
string c_j(string str);
string c_c(string str);
string c_e(string str);
string c_a(string str);
string c_p(string str);
string c_m(string str);
int main() {
  int n;
  string order, message;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> order >> message;
    string ans = message;
    for (int j = order.length() - 1; j >= 0; j--) {
      if (order[j] == 'J')
        ans = c_j(ans);
      else if (order[j] == 'C')
        ans = c_c(ans);
      else if (order[j] == 'E')
        ans = c_e(ans);
      else if (order[j] == 'A')
        ans = c_a(ans);
      else if (order[j] == 'P')
        ans = c_p(ans);
      else if (order[j] == 'M')
        ans = c_m(ans);
    }
    cout << ans << endl;
  }
  return 0;
}
string c_j(string str) {
  char c;
  c = str[str.length() - 1];
  str.erase(str.end() - 1);
  str = c + str;
  return str;
}
string c_c(string str) {
  char c;
  c = str[0];
  str.erase(str.begin());
  str += c;
  return str;
}
string c_e(string str) {
  string a;
  int l = str.length();
  a = str.substr(0, l / 2);
  if (l % 2 == 1) {
    char c = str[l / 2];
    str.erase(str.begin(), str.begin() + (l / 2) + 1);
    str += c;
    str += a;
  } else {
    str.erase(str.begin(), str.begin() + (l / 2));
    str += a;
  }
  return str;
}
string c_a(string str) {
  reverse(str.begin(), str.end());
  return str;
}
string c_p(string str) {
  for (int i = 0; i < str.length() - 1; i++) {
    if (isdigit(str[i])) {
      int n = str[i] - '0';
      n--;
      if (n == -1) n = 9;
      str[i] = n + '0';
    }
  }
  return str;
}
string c_m(string str) {
  for (int i = 0; i < str.length() - 1; i++) {
    if (isdigit(str[i])) {
      int n = str[i] - '0';
      n = (n + 1) % 10;
      str[i] = n + '0';
    }
  }
  return str;
}
