#include <cstdio>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <array>

using namespace std;

#define all(c) begin(c), end(c)
#define dump(c) cerr << "> " << #c << " = " << (c) << endl
#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const double EPS = 1e-10;


template<typename T1,typename T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p){
  return os << '(' << p.first <<',' << p.second << ')';
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a){
  os << '[';
  rep(i,a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  int N; cin >> N;
  while(N--) {
    string O, M; cin >> O >> M;
    reverse(all(O));
    for(char o : O) {
      if(o == 'J') {
        M = M.substr(M.size() - 1) + M.substr(0, M.size() - 1);
      }
      if(o == 'C') {
        M = M.substr(1) + M.substr(0, 1);
      }
      if(o == 'E') {
        int h = M.size() / 2;
        M = M.substr(M.size() - h) + M.substr(h, M.size() & 1) + M.substr(0, h);
      }
      if(o == 'A') {
        reverse(all(M));
      }
      if(o == 'P') {
        for(char &c : M) if(isdigit(c)) c = (c - '0' - 1 + 10) % 10 + '0';
      }
      if(o == 'M') {
        for(char &c : M) if(isdigit(c)) c = (c - '0' + 1) % 10 + '0';
      }
    }

    cout << M << endl;
  }
  
  
  return 0;
}