#include <bits/stdc++.h>
using namespace std;
string f_J(string s) {
  char c = s[0];
  for (int i = 0; i < s.size() - 1; i++) {
    s[i] = s[i + 1];
  }
  s[s.size() - 1] = c;
  return s;
}
string f_C(string s) {
  char c = s[s.size() - 1];
  for (int i = s.size() - 1; i > 0; i--) {
    s[i] = s[i - 1];
  }
  s[0] = c;
  return s;
}
string f_E(string s) {
  int n = s.size();
  if (n % 2 == 0) {
    string r = "", l = "";
    for (int i = 0; i < n / 2; i++) {
      l = l + s[i];
    }
    for (int i = n / 2; i < n; i++) {
      r = r + s[i];
    }
    s = r + l;
  } else {
    string r = "", l = "";
    for (int i = 0; i < n / 2; i++) {
      r = r + s[i];
    }
    for (int i = n / 2 + 1; i < n; i++) {
      l = l + s[i];
    }
    s = l + s[n / 2] + r;
  }
  return s;
}
string f_A(string s) {
  reverse(s.begin(), s.end());
  return s;
}
string f_P(string s) {
  for (int i = 0; i < s.size(); i++) {
    if ('0' <= s[i] && s[i] <= '8') {
      s[i]++;
    } else if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}
string f_M(string s) {
  for (int i = 0; i < s.size(); i++) {
    if ('1' <= s[i] && s[i] <= '9') {
      s[i]--;
    } else if (s[i] == '0') {
      s[i] = '9';
    }
  }
  return s;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    string s;
    cin >> s;
    for (int j = 0; j < t.size(); j++) {
      if (t[j] == 'J') s = f_J(s);
      if (t[j] == 'C') s = f_C(s);
      if (t[j] == 'E') s = f_E(s);
      if (t[j] == 'A') s = f_A(s);
      if (t[j] == 'P') s = f_P(s);
      if (t[j] == 'M') s = f_M(s);
    }
    cout << s << endl;
  }
  return 0;
}
