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

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

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

void half(string& a)
{
	int N = a.size();
	for(int i=0; i<N/2; i++) swap(a[i], a[N/2+i+N%2]);
}

int main()
{
	int N;
	cin >> N;
	while(N--) {
		string cmd, str;
		cin >> cmd;
		reverse(cmd.begin(), cmd.end());

		cin >> str;
		for(int i=0; i<cmd.size(); i++) {
			if(cmd[i] == 'A') reverse(str.begin(), str.end());
			if(cmd[i] == 'M') inc(str);
			if(cmd[i] == 'P') dec(str);
			if(cmd[i] == 'C') rotate(str.begin(), str.begin() + 1, str.end());
			if(cmd[i] == 'J') rotate(str.begin(), str.end() - 1, str.end());
			if(cmd[i] == 'E') half(str);
		}

		cout << str << endl;
	}


}