#include <cctype>
#include <iostream>
#include <string>
using namespace std;

void solve() {
	string order, message;
	cin >> order >> message;
	for (int i = order.size() - 1; i >= 0; i--) {
		int l;
		switch (order[i]) {
		case 'J':
			message = (*message.rbegin()) + message.substr(0, message.size() - 1);
			break;
		case 'C':
			message = message.substr(1) + message[0];
			break;
		case 'E':
			l = message.size() / 2;
			if (message.size() & 1) {
				message = message.substr(message.size() - l) + message[l] + message.substr(0, l);
			}
			else {
				message = message.substr(message.size() - l) + message.substr(0, l);
			}
			break;
		case 'A':
			message = string(message.rbegin(), message.rend());
			break;
		case 'P':
			for (unsigned j = 0; j < message.size(); j++) {
				if (isdigit(message[j])) {
					if (message[j] == '0') {
						message[j] = '9';
					}
					else {
						message[j]--;
					}
				}
			}
			break;
		case 'M':
			for (unsigned j = 0; j < message.size(); j++) {
				if (isdigit(message[j])) {
					if (message[j] == '9') {
						message[j] = '0';
					}
					else {
						message[j]++;
					}
				}
			}
			break;
		}
	}
	cout << message << endl;
}

int main () {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		solve();
	}
	return 0;
}