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

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	while(n--) {
		string ope, message;
		cin >> ope >> message;

		const int l = message.size();
		for(int i = ope.size() - 1; i >= 0; --i) {
			switch(ope[i]) {
			case 'A':
				reverse(message.begin(), message.end());
				break;
			case 'C':
				rotate(message.begin(), message.begin() + 1, message.end());
				break;
			case 'E':
				message = message.substr((l + 1) / 2)
					+ (l & 1 ? string(1, message[l / 2]) : "")
					+ message.substr(0, l / 2);
				break;
			case 'J':
				rotate(message.begin(), message.begin() + l - 1, message.end());
				break;
			case 'M':
				for(int j = 0; j < (int)message.size(); ++j)
					if(isdigit(message[j]))
						message[j] = (message[j] - '0' + 1) % 10 + '0';
				break;
			case 'P':
				for(int j = 0; j < (int)message.size(); ++j)
					if(isdigit(message[j]))
						message[j] = (message[j] - '0' + 10 - 1) % 10 + '0';
			}
		}

		cout << message << endl;
	}

	return EXIT_SUCCESS;
}