#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;

  while ( n-- ) {
    string ord, mes;
    cin >> ord >> mes;

    int l = (int)mes.size();    
    for ( int i = (int)ord.size()-1; i >= 0; i-- ) {      
      if ( ord[i] == 'J' ) {
	int c = mes[l-1];
	for ( int i = l-1; i > 0; i-- ) mes[i] = mes[i-1];
	mes[0] = c;	
      } else if ( ord[i] == 'C' ) {
	int c = mes[0];
	for ( int i = 0; i < l-1; i++ ) mes[i] = mes[i+1];
	mes[l-1] = c;	
      } else if ( ord[i] == 'E' ) {
	string a = "", b = "", c = "";	
	for ( int i = 0; i < l; i++ ) {
	  if ( l%2 && i == l/2 ) b += mes[i];
	  else if ( i < l/2 ) c += mes[i];
	  else a += mes[i];	  
	}
	mes = a+b+c;	
      } else if ( ord[i] == 'A' ) {
	reverse(mes.begin(), mes.end());	
      } else if ( ord[i] == 'P' ) {	
	for ( int i = 0; i < l; i++ ) {
	  if ( mes[i] == '0' ) mes[i] = '9';
	  else if ( isdigit(mes[i]) ) mes[i]--;	  
	}	  
      } else {
	for ( int i = 0; i < l; i++ ) {
	  if ( mes[i] == '9' ) mes[i] = '0';
	  else if ( isdigit(mes[i]) ) mes[i]++;	  
	}
      }      
    }

    cout << mes << endl;
  }
  
  return 0;
}

