#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

void inv_A ( string& str )
{
	reverse( str.begin(), str.end() );
}

void inv_C ( string& str )
{
	str = str.substr( 1 ) + str[0];
}

void inv_E ( string& str )
{
	int half = str.size() / 2, offset = ( str.size() + 1 ) / 2;
	for ( int i = 0; i < half; ++i ) {
		swap( str[i], str[i + offset] );
	}
}

void inv_J ( string& str )
{
	str = str[str.size() - 1 ] + str.substr( 0, str.size() - 1 );
}

void inv_M ( string& str )
{
	for ( int i = 0; i < str.size(); ++i ) {
		if ( isdigit( str[i] ) ) {
			str[i] = '0' + ( str[i] - '0' + 1 ) % 10;
		}
	}
}

void inv_P ( string& str )
{
	for ( int i = 0; i < str.size(); ++i ) {
		if ( isdigit( str[i] ) ) {
			str[i] = '0' + ( str[i] - '0' + 9 ) % 10;
		}
	}
}

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

	while ( n-- ) {
		string order,
			   message;
		cin >> order
			>> message;

		for ( int i = order.size() - 1; i >= 0; --i ) {
			switch ( order[i] ) {
			case 'A':
				inv_A( message );
				break;
			case 'C':
				inv_C( message );
				break;
			case 'E':
				inv_E( message );
				break;
			case 'J':
				inv_J( message );
				break;
			case 'M':
				inv_M( message );
				break;
			case 'P':
				inv_P( message );
				break;
			}
			// cout << i << ' ' << message << endl;
		}
		cout << message << endl;
	}
	return 0;
}