#include<iostream>
#include<string>
using namespace std;
string s, o;
void J() {
	string st = s[s.size() - 1] + s.substr(0, s.size() - 1);
	s = st;
	return;
}
void C() {
	string st = s.substr(1, s.size() - 1) + s[0];
	s = st;
	return;
}
void E() {
	string a, b, st;
	int k = (s.size() - s.size() % 2) / 2;
	a = s.substr(0, k);
	if (s.size() % 2)st = s[k];
	b = s.substr(k + s.size() % 2, k);
	s = b + st + a;
	return;
}
void A() {
	string st;
	for (int i = s.size() - 1; i >= 0; i--)st += s[i];
	s = st;
	return;
}
void P() {
	string st;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '0')st += '9';
		else if (s[i] == '1')st += '0';
		else if (s[i] == '2')st += '1';
		else if (s[i] == '3')st += '2';
		else if (s[i] == '4')st += '3';
		else if (s[i] == '5')st += '4';
		else if (s[i] == '6')st += '5';
		else if (s[i] == '7')st += '6';
		else if (s[i] == '8')st += '7';
		else if (s[i] == '9')st += '8';
		else st += s[i];
	}
	s = st;
	return;
}
void M() {
	string st;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '0')st += '1';
		else if (s[i] == '1')st += '2';
		else if (s[i] == '2')st += '3';
		else if (s[i] == '3')st += '4';
		else if (s[i] == '4')st += '5';
		else if (s[i] == '5')st += '6';
		else if (s[i] == '6')st += '7';
		else if (s[i] == '7')st += '8';
		else if (s[i] == '8')st += '9';
		else if (s[i] == '9')st += '0';
		else st += s[i];
	}
	s = st;
	return;
}
int main() {
	int n; cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> o >> s;
		for (int j = o.size() - 1; j > -1; j--) {
			if (o[j] == 'J')J();
			else if (o[j] == 'C')C();
			else if (o[j] == 'E')E();
			else if (o[j] == 'A')A();
			else if (o[j] == 'P')P();
			else if (o[j] == 'M')M();
		}
		cout << s << endl;
	}
}