#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int main()
{
	int n;

	cin >> n;

	rep(loop, n) {
		string messengers;
		string message;

		cin >> messengers >> message;

		rep(i, messengers.size()) {
			switch (messengers[messengers.size() - 1 - i]) {
			case 'J':
				message = message.substr(message.size() - 1, 1) + message.substr(0, message.size() - 1);
				break;
			case 'C':
				message = message.substr(1) + message.substr(0, 1);
				break;
			case 'E':
				if (message.size() % 2 == 0) {
					message = message.substr(message.size() / 2, message.size() / 2) + message.substr(0, message.size() / 2);
				}
				else {
					message = message.substr(message.size() / 2 + 1, message.size() / 2) + message.substr(message.size() / 2, 1) + message.substr(0, message.size() / 2);
				}
				break;
			case 'A':
				rep(j, message.size() / 2) {
					swap(message[j], message[message.size() - 1 - j]);
				}
				break;
			case 'P':
				rep(j, message.size()) {
					if (message[j] == '0') {
						message[j] = '9';
					}
					else if (message[j] >= '1'&&message[j] <= '9') {
						message[j]--;
					}
				}
				break;
			case 'M':
				rep(j, message.size()) {
					if (message[j] == '9') {
						message[j] = '0';
					}
					else if (message[j] >= '0'&&message[j] <= '8') {
						message[j]++;
					}
				}
				break;
			}
		}

		cout << message << endl;
	}

	return 0;
}