//????¨???????????????????????????????????????????.
//????????????????????????????????°??????.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int n;
string people;
string result;

string rshift(string s) {
	string ret;
	ret += s[s.length() - 1];
	for (int i = 0; i < s.length() - 1; i++) ret += s[i];
	return ret;
}

string lshift(string s) {
	string ret;
	for (int i = 1; i < s.length(); i++) ret += s[i];
	ret += s[0];
	return ret;
}

string swaplr(string s) {
	string pre, suf;
	for (int i = 0; i < s.length() / 2; i++) pre += s[i];
	for (int i = 0; i < s.length() / 2; i++) suf += s[s.length() / 2 + s.length() % 2 + i];
	if (s.length() % 2) return suf + s[s.length() / 2] + pre;
	return suf + pre;
}

string reverse(string s) {
	int l = 0, r = s.length() - 1;
	while (l < r) {
		swap(s[l], s[r]);
		l++;
		r--;
	}
	return s;
}

string decrements(string s) {
	for (int i = 0; i < s.length(); i++) {
		if ('0' <= s[i] && s[i] <= '9') {
			if (s[i] == '0') s[i] = '9';
			else s[i]--;
		}
	}
	return s;
}

string increments(string s) {
	for (int i = 0; i < s.length(); i++) {
		if ('0' <= s[i] && s[i] <= '9') {
			if (s[i] == '9') s[i] = '0';
			else s[i]++;
		}
	}
	return s;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> people >> result;
		for (int j = people.length() - 1; j >= 0; j--) {
			char c = people[j];
			if (c == 'J') result = rshift(result);
			if (c == 'C') result = lshift(result);
			if (c == 'E') result = swaplr(result);
			if (c == 'A') result = reverse(result);
			if (c == 'P') result = decrements(result);
			if (c == 'M') result = increments(result);
		}
		cout << result << endl;
	}
	return 0;
}