// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1240

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

int main() {
  int N;
  cin >> N;
  for (int i=0; i<N; i++) {
    string S, O;
    cin >> O >> S;
    reverse(O.begin(), O.end());
    for (auto i=0; i<O.size(); i++) {
      switch(O[i]) {
      case 'J':
	S = S[S.size()-1] + S.substr(0, S.size()-1);
	break;
      case 'C':
	S = S.substr(1) + S[0];
	break;
      case 'E':
	if (S.size()%2 == 0) {
	  S = S.substr(S.size()/2) + S.substr(0, S.size()/2);
	} else {
	  S = S.substr(S.size()/2+1) + S[S.size()/2] 
	    + S.substr(0, S.size()/2);
	}
	break;
      case 'A':
	reverse(S.begin(), S.end());
	break;
      case 'P':
	for (auto i=0; i<S.size(); i++) {
	  if (isdigit(S[i])) {
	    S[i] = (char)((((int)S[i] - '0')+10 -1)%10 + '0');  
	  }
	}
	break;
      case 'M':
	for (auto i=0; i<S.size(); i++) {
	  if (isdigit(S[i])) {
	    S[i] = (char)((((int)S[i] - '0')+10 +1)%10 + '0');  
	  }
	}
	break;
      default:
	break;
      }
    }
    cout << S << endl;
  }
}