#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
using namespace std;

typedef ostringstream oss;
typedef istringstream iss;

string J(string str) {
	str += str[0];
	str.erase(str.begin());
	return str;
}

string C(string str) {
	str.insert(str.begin(), *(str.rbegin()));
	str.erase(str.size() - 1);
	return str;
}

string E(string str) {
	for (int i = 0; i < str.size() / 2; i++) {
		swap(str[i], str[(str.size() + 1) / 2  + i]);
	}
	return str;
}

string A(string str) {
	for (int i = 0; i < str.size() / 2; i++) {
		swap(str[i], str[str.size() - 1 - i]);
	}
	return str;
}

string P(string str) {
	for (int i = 0; i < str.size(); i++) {
		if (str[i] >= '0' && str[i] <= '8') {
			str[i]++;
		}else if (str[i] == '9') {
			str[i] = '0';
		}
	}
	return str;
}

string M(string str) {
	for (int i = 0; i < str.size(); i++) {
		if (str[i] >= '1' && str[i] <= '9') {
			str[i]--;
		}else if (str[i] == '0') {
			str[i] = '9';
		}
	}
	return str;
}

struct pFunc
{
	pFunc(){}
	pFunc(string (*p)(string str)) : f(p) {}
	string (*f)(string str);
};


int main(void) {
	map<char, pFunc> F;
	F['J'] = pFunc(C);
	F['C'] = pFunc(J);
	F['E'] = pFunc(E);
	F['A'] = pFunc(A);
	F['P'] = pFunc(M);
	F['M'] = pFunc(P);

	int N;
	cin >> N;
	cin.ignore();
	for (; N; N--) {
		string str, ope;
		getline(cin, ope);
		getline(cin, str);
		for (int i = ope.size() - 1; i >= 0; i--) {
			str = F[ope[i]].f(str);
		}
		cout << str << endl;
	}


	return 0;
}