#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
	int N; cin >> N;
	while (N--) {
		string st; cin >> st;
		string aa; cin >> aa;
		for (int i = 0; i < st.size(); ++i) {
			char c = st[st.size() - 1 - i];
			if (c == 'A') {
				reverse(aa.begin(), aa.end());
			}
			else if (c == 'C') {
				rotate(aa.begin(), aa.begin() + 1, aa.end());
			}
			else if (c == 'M') {
				for (auto& ac : aa) {
					if (isdigit(ac))ac = (ac - '0' + 1) % 10 + '0';
				}
			}
			else if (c == 'P') {
				for (auto& ac : aa) {
					if (isdigit(ac))ac = (ac - '0' + 9) % 10 + '0';
				}
			}
			else if (c == 'E') {
				string from = aa.substr(0,aa.size() / 2);
				string to = aa.substr(aa.size()-aa.size() / 2,aa.size() / 2);
				if (aa.size() % 2)aa = to + aa[aa.size() / 2] + from;
				else aa = to + from;
			}
			else if (c == 'J') {

				rotate(aa.begin(), aa.begin() + aa.size()-1, aa.end());
			}
		}
		cout << aa << endl;
	}
	return 0;
}