#include <bits/stdc++.h>
using namespace std;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long LINF = numeric_limits<long long>::max() / 3;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream& operator<<(ostream& os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  os << "[";
  for (const auto& v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& st) {
  os << "{";
  for (T v : st) os << v << ",";
  os << "}";
  return os;
}
template <typename T, typename U>
inline void chmax(T& x, U y) {
  if (y > x) x = y;
}
template <typename T, typename U>
inline void chmin(T& x, U y) {
  if (y < x) x = y;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
constexpr double eps = 1e-10;
constexpr long long mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
string J(string t) {
  string ret;
  ret.push_back(t[t.size() - 1]);
  for (int i = 0; i < (int)(t).size() - 1; i++) {
    ret.push_back(t[i]);
  }
  return ret;
}
string C(string t) {
  string ret;
  for (int i = 1; i < (int)(t).size(); i++) {
    ret.push_back(t[i]);
  }
  ret.push_back(t[0]);
  return ret;
}
string E(string s) {
  if (s.size() % 2 == 0) {
    int t = s.size();
    string s1 = s.substr(0, t / 2);
    string s2 = s.substr(t / 2, t / 2);
    return s2 + s1;
  } else {
    int t = s.size();
    string s1 = s.substr(0, t / 2);
    string s2 = s.substr(t / 2 + 1, t / 2);
    char c = s[t / 2];
    string ret = s2;
    ret.push_back(c);
    ret += s1;
    return ret;
  }
}
string A(string s) {
  string ret = s;
  reverse((ret).begin(), (ret).end());
  return ret;
}
string P(string s) {
  for (int i = 0; i < (int)(s).size(); i++) {
    if ('1' <= s[i] and s[i] <= '9')
      s[i]--;
    else if (s[i] == '0')
      s[i] == '9';
  }
  return s;
}
string M(string s) {
  for (int i = 0; i < (int)(s).size(); i++) {
    if ('0' <= s[i] and s[i] < '9') {
      s[i]++;
    } else if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}
void solve() {
  string s, t;
  cin >> s >> t;
  for (int i = (int)(s).size() - 1; i >= 0; i--) {
    string u;
    if (s[i] == 'J') t = J(t);
    if (s[i] == 'C') t = C(t);
    if (s[i] == 'E') t = E(t);
    if (s[i] == 'A') t = A(t);
    if (s[i] == 'P') t = P(t);
    if (s[i] == 'M') t = M(t);
  }
  cout << t << endl;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) solve();
}
