#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main() {
	int n; cin >> n;
	while (n--) {
		string s, message; cin >> s >> message;
		int msz = message.size();
		for (int i = s.size() - 1; i >= 0; --i) {
			if (s[i] == 'J') {
				message = message[msz - 1] + message.substr(0, msz - 1);
			} else if (s[i] == 'C') {
				message = message.substr(1, msz - 1) + message[0];
			} else if (s[i] == 'E') {
				for (int j = 0; j < msz / 2; ++j) swap(message[j], message[j + (msz + 1) / 2]);
			} else if (s[i] == 'A') {
				reverse(message.begin(), message.end());
			} else if (s[i] == 'P') {
				for (int j = 0; j < msz; ++j)
					if (isdigit(message[j])) message[j] = ((message[j] - '0' + 9) % 10) + '0';
			} else if (s[i] == 'M') {
				for (int j = 0; j < msz; ++j)
					if (isdigit(message[j])) message[j] = ((message[j] - '0' + 1) % 10) + '0';
			}
		}
		cout << message << endl;
	}

	return 0;
}
