#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main(){
  int n;
  cin>>n;
  while(n--){
    string s,t;
    cin>>s>>t;
    for(int i = s.size() - 1; i >= 0; i--){
      if(s[i] == 'C') t = t.substr(1) + t.substr(0, 1);
      if(s[i] == 'J') t = t.substr(t.size()-1) + t.substr(0, t.size()-1);
      if(s[i] == 'E'){
        if(t.size() % 2) t = t.substr(t.size() / 2 + 1) + t.substr(t.size()/2, 1) + t.substr(0, t.size()/2);
        else t = t.substr(t.size()/2) + t.substr(0, t.size()/2);
      }
      if(s[i] == 'A') reverse(t.begin(), t.end());
      if(s[i] == 'M') REP(j, t.size())if(isdigit(t[j])) t[j] = (t[j]-'0' + 1) % 10 + '0';
      if(s[i] == 'P') REP(j, t.size())if(isdigit(t[j])) t[j] = (t[j]-'0' + 9) % 10 + '0';
    }
    cout<<t<<endl;
  }
  return 0;
}