#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using ull=unsigned long long int;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int main(){
int t;
cin>>t;
while(t--){
  string q;
  cin>>q;
  string s;
  cin>>s;
  int n=q.size(),m=s.size();
  reverse(all(q));
  for(int i=0;i<n;i++){
    string tmp={};
    if(q[i]=='J'){
      for(int j=0;j<m;j++){
        tmp.push_back(s[(j-1+m)%m]);
      }
      s=tmp;
    }
    if(q[i]=='C'){
      for(int j=0;j<m;j++){
        tmp.push_back(s[(j+1)%m]);
      }
      s=tmp;
    }
    if(q[i]=='E'){
      for(int j=(m+1)/2;j<m;j++){
        tmp.push_back(s[j]);
      }
      if(m%2==1)tmp.push_back(s[m/2]);
      for(int j=0;j<m/2;j++){
        tmp.push_back(s[j]);
      }
      s=tmp;
    }
    if(q[i]=='A'){
      reverse(all(s));
    }
    if(q[i]=='P'){
      for(int j=0;j<m;j++){
        if(int(s[j]-'0')<=10){
          s[j]=char((int(s[j]-'0')+9)%10+'0');
        }
      }
    }
     if(q[i]=='M'){
      for(int j=0;j<m;j++){
        if(int(s[j]-'0')<=10){
          s[j]=char((int(s[j]-'0')+1)%10+'0');
        }
      }
    }
  }
  cout<<s<<"\n";
}
}
