#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	int N; cin >> N;
	for (int t = 0; t < N; ++t) {
		string s1, s2; cin >> s1 >> s2;
		reverse( s1.begin(), s1.end() );
		for (int i = 0; i < s1.size(); ++i) {
			switch (s1[i]) {
				case 'A':
					reverse( s2.begin(), s2.end() );
					break;

				case 'J':
					s2 = s2[s2.size()-1] + s2.substr(0, s2.size()-1);
					break;

				case 'C':
					s2 = s2.substr(1) + s2[0];
					break;

				case 'E':
					if (s2.size() > 1) {
						if (s2.size() % 2 == 0) s2 = s2.substr(s2.size()/2) + s2.substr(0, s2.size()/2);
						else s2 = s2.substr(s2.size()/2+1) + s2[s2.size()/2] + s2.substr(0, s2.size()/2);
					}
					break;

				case 'M':
					for (int j = 0; j < s2.size(); ++j) {
						if ('0' <= s2[j] && s2[j] < '9') ++s2[j];
						else if (s2[j] == '9') s2[j] = '0';
					}
					break;

				case 'P':
					for (int j = 0; j < s2.size(); ++j) {
						if ('0' < s2[j] && s2[j] <= '9') --s2[j];
						else if (s2[j] == '0') s2[j] = '9';
					}
					break;
			}
		}
		cout << s2 << endl;
	}
}