#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main () {
  int n;

  cin >> n;
  for( int i = 0; i < n; i++ ) {
    string commands, mes;
    cin >> commands >> mes;
    int len = commands.length();
    int meslen = mes.length();
    for( int j = len - 1; j >= 0; j-- ) {
      switch( commands[ j ] ) {
        case 'C':
          mes = mes.substr( 1 ) + mes[ 0 ];
          break;
        case 'J':
          mes = mes[ meslen - 1 ] + mes.substr( 0, meslen - 1 );
          break;
        case 'E':
          if( meslen % 2 == 1 ) {
            mes = 
              mes.substr( meslen / 2 + 1 ) + 
              mes[ meslen / 2 ] +
              mes.substr( 0, meslen / 2 );
          }else{
            mes = 
              mes.substr( meslen / 2 ) +
              mes.substr( 0, meslen / 2 ); 
          }
          break;
        case 'A':
          reverse( mes.begin(), mes.end() );
          break;
        case 'M':
          for( int k = 0; k < meslen; k++ ) {
            if( '0' <= mes[ k ] && mes[ k ] <= '8' ){
              mes[ k ]++;
            }else if( mes[ k ] == '9' ){
              mes[ k ] = '0';
            }
          }
          break;
        case 'P':
          for( int k = 0; k < meslen; k++ ) {
            if( '1' <= mes[ k ] && mes[ k ] <= '9' ){
              mes[ k ]--;
            }else if( mes[ k ] == '0' ){
              mes[ k ] = '9';
            }
          }
          break;
      }
    }
    cout << mes << endl;
  }

  return EXIT_SUCCESS;
}