#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
void chmes(string &s,char me){
  if(me == 'C'){
    string tmp="";
    for(int i=1;i<s.size();i++){
      tmp+=s[i];
    }
    tmp += s[0];
    s = tmp;
  }else if(me == 'J'){
    string tmp="";
    tmp+=s.back();
    for(int i=0;i<s.size()-1;i++){
      tmp+=s[i];
    }
    s = tmp;
  }else if(me == 'E'){
    if(s.size()%2 == 1){
      string tmp1="",tmp2="";
      for(int i=0;i<s.size()/2;i++){
	tmp1+=s[i];
	tmp2+=s[s.size()/2+i+1];
      }
      char hge = s[s.size()/2];
      s = tmp2;
      s+= hge;
      s += tmp1;
    }else{
      string tmp1="",tmp2="";
      for(int i=0;i<s.size()/2;i++){
	tmp1+=s[i];
	tmp2+=s[s.size()/2+i];
      }
      s = tmp2+tmp1;
      // cout<<"!"<<s<<endl;
    }
  }else if(me == 'A'){
    reverse(ALL(s));
  }else if(me == 'M'){
    for(int i=0;i<s.size();i++){
      if(isdigit(s[i])){
	s[i] = (char)('0'+(s[i]-'0'+1)%10);
      }
    }
  }else{
    for(int i=0;i<s.size();i++){
      if(isdigit(s[i])){
	s[i] = (char)('0'+(s[i]-'0'+9)%10);
      }
    }
  }
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;cin>>n;
  for(int i=0;i<n;i++){
    string s,ss;
    cin>>s>>ss;
    reverse(ALL(s));
    for(auto a:s){
      chmes(ss,a);
    }
    cout<<ss<<endl;
  }
  return 0;
}

