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

int main() {

  long long int n;
  cin >> n;

  for ( long long int i = 0; i < n; i++ ) {

    string o, in;
    cin >> o >> in;

    for ( long long int j = o.size() - 1; j >= 0; j-- ) {

      if ( o[j] == 'J' ) {
	in = in[ in.size() - 1 ] + in.substr( 0, in.size() - 1 );
      }else if ( o[j] == 'C' ) {
	in = in.substr( 1, in.size() - 1 ) + in[0];
      }else if ( o[j] == 'E' ) {
	long long int k = in.size() / 2;
	if ( ( in.size() % 2 ) == 0 ) {
	  in = in.substr( k, k ) + in.substr( 0, k );
	}else {
	  in = in.substr( k + 1, k ) + in[k] + in.substr( 0, k );
	}
      }else if ( o[j] == 'A' ) {
	string s = "";
	for ( long long int x = in.size() - 1; x >= 0; x-- ) {
	  s += in[x];
	}
	in = s;
      }else if ( o[j] == 'M' ) {
	for ( long long int x = 0; x < in.size(); x++ ) {
	  if ( in[x] >= '0' && in[x] < '9' ) {
	    in[x]++;
	  }else if ( in[x] == '9' ) {
	    in[x] = '0';
	  }
	}
      }else if ( o[j] == 'P' ) {
	for ( long long int x = 0; x < in.size(); x++ ) {
	  if ( in[x] > '0' && in[x] <= '9' ) {
	    in[x]--;
	  }else if ( in[x] == '0' ) {
	    in[x] = '9';
	  }
	}
      }

    }

    cout << in << endl;

  }

  return 0;

}