#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

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

int main(void) {
  int i,j,k;
  int n;
  cin>>n;
  rep(i,n){
    string ord,s;
    cin>>ord;
    cin>>s;
    int len=s.size();
    for(j=ord.size()-1;j>=0;j--){
      if(ord[j]=='C'){
	string tmp="";
	tmp.push_back(s[0]);
	s.erase(s.begin());
	s+=tmp;
      }else if(ord[j]=='J'){
	string tmp="";
	tmp.push_back(s[len-1]);
	s.erase(s.end()-1);
	s=tmp+s;
      }else if(ord[j]=='A'){
	reverse(all(s));
      }else if(ord[j]=='E'){
	rep(k,len/2)
	  swap(s[k],s[(len+1)/2+k]);
      }else if(ord[j]=='M'){
	rep(k,len)
	  if(s[k]<='8' && s[k]>='0')s[k]++;
	  else if(s[k]=='9')s[k]='0';
      }else{
	rep(k,len)
	  if(s[k]<='9' && s[k]>'0')s[k]--;
	  else if(s[k]=='0')s[k]='9';
      }
    }
    cout<<s<<endl;
  }
}