#include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

void c_j(string str);
void c_c(string str);
void c_e(string str);
void c_a(string str);
void c_p(string str);
void c_m(string str);

int main()
{
	int n;
	string order, message;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> order >> message;
		for(int j = order.length()-1; j >= 0; j++){
			if(order[j] == 'J') c_j(message);
			else if(order[j] == 'C') c_c(message);
			else if(order[j] == 'E') c_e(message);
			else if(order[j] == 'A') c_a(message);
			else if(order[j] == 'P') c_p(message);
			else if(order[j] == 'M') c_m(message); 
		}
		cout << message << endl;
	}
	return 0;
}

void c_j(string str)
{
	char c;
	c = str[str.length()-1];
	str.pop_back;
	str = c + str;
}

void c_c(string str)
{
	char c;
	c = str[0];
	str.erase(str.begin());
	str += c;
}

void c_e(string str)
{
	string a;
	int l = str.length();
	a = str.substr(0, l / 2);
	if( l % 2 == 1){
		char c = str[l / 2 + 1];
		str.erase(str.begin(), str.begin() + l);
		str += c;
		str += a;
	} else {
		str.erase(str.begin(), str.begin() + (l - 1));
		str += a;
	}
}

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

void c_p(string str)
{
	for(int i = 0; i < str.length() - 1; i++){
		if(isdigit(str[i])){
			int n = str[i] - '0';
			n--;
			if(n == -1) n = 9;
			str[i] = n + '0';
		}
	}
}

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