#include <iostream>
using namespace std;

int main()
{
	int n;
	string intro,str;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> intro >> str;
		for (int j = intro.length() - 1; j >= 0; j--) {
			switch (intro[j])
			{
			case 'A': {
				string res;
				res.reserve(str.length());
				for (int k = str.length() - 1; k >= 0; k--) {
					res.push_back(str[k]);
				}
				str = res;
			}
				break;
			case 'C': {
				string res;
				res.reserve(str.length());
				for (int k = 0; k < str.length(); k++) {
					res.push_back(str[(k + 1) % str.length()]);
				}
				str = res;
			}
					  break;
			case 'E': {
				string res;
				res.reserve(str.length());
				for (int k = 0; k < str.length(); k++) {
					if (k < str.length() / 2) {
						res.push_back(str[k + str.length() - str.length() / 2]);
					}
					else if (k >= str.length() - str.length() / 2) {
						res.push_back(str[k - (str.length() - str.length() / 2)]);
					}
					else {
						res.push_back(str[k]);
					}
				}
				str = res;
			}
					  break;
			case 'J': {
				string res;
				res.reserve(str.length());
				for (int k = 0; k < str.length(); k++) {
					res.push_back(str[(k + str.length() - 1) % str.length()]);
				}
				str = res;
			}
				break;
			case 'M': {
				for (int k = 0;k < str.length(); k++) {
					if (str[k] <= '9' && str[k] >= '0') {
						str[k] = '0' + (str[k] - '0' + 1) % 10;
					}
				}
			}
				break;
			case 'P': {
				for (int k = 0; k < str.length(); k++) {
					if (str[k] <= '9' && str[k] >= '0') {
						str[k] = '0' + (str[k] - '0' + 9) % 10;
					}
				}
			}
				break;
			}
		}
		cout << str << endl;
	}
	return 0;
}