#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define DEB 0
#define all(x) x.begin(), x.end()

int main(){
  int n;
  string order, mess, tmp;
  cin>>n;
  while(n--){
    cin >> order >> mess;
    for(int i=order.size()-1; i>=0; i--){
      switch(order[i]){
      case 'C':
	mess = mess.substr(1) + mess.substr(0,1);
	break;
      case 'J':
	mess = mess.substr(mess.size()-1) + mess.substr(0,mess.size()-1);
	break;
      case 'E':
	if( mess.size()%2==0 ){
	  mess = mess.substr(mess.size()/2) + mess.substr(0,mess.size()/2);
	}else{
	  mess = mess.substr(mess.size()/2+1) + mess[mess.size()/2] + mess.substr(0,mess.size()/2);
	}
	break;
      case 'A':
	reverse(all(mess));
	break;
      case 'P':
	rep(j,mess.size()){
	  if( '0'<=mess[j] && mess[j]<='9' ){
	    if( mess[j]=='0' ){
	      mess[j] = '9';
	    }else{
	      mess[j]--;
	    }
	  }
	}
	break;
      case 'M':
	rep(j,mess.size()){
	  if( '0'<=mess[j] && mess[j]<='9' ){
	    if( mess[j]=='9' ){
	      mess[j] = '0';
	    }else{
	      mess[j]++;
	    }
	  }
	}
	break;
      }
#if DEB
      cout << ":: " << mess << endl;
#endif
    }
    cout << mess << endl;
  }
  return 0;
}