#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int z = 0; z < n; z++) {
		string a, b;
		cin >> a >> b;
		for (int i = a.length() - 1; i >= 0; i--) {
			string c = "";
			if (a[i] == 'C') {
				for (int i = 1; i < b.length(); i++) c += b[i];
				c += b[0];
				b = c;
			}
			if (a[i] == 'J') {
				c += b[b.length() - 1];
				for (int i = 0; i < b.length() - 1; i++) c += b[i];
				b = c;
			}
			if (a[i] == 'E') {
				int d = floor(b.length() / 2.0);
				string e = "", f = "", g = "";
				int h;
				if (b.length() % 2 == 0) h = b.length()/ 2;
				else h = d+1;
				for (int i = 0; i < d; i++) {
					e += b[i];
				}
				if(b.length()%2!=0)f = b[d];
				for (int i = h; i < b.length(); i++) {
					g += b[i];
				}
				c += g += f += e;
				b = c;
			}
			if (a[i] == 'A') {
				for (int i = b.length() - 1; i >= 0; i--) c += b[i];
				b = c;
			}
			if (a[i] == 'M') {
				for (int i = 0; i < b.length(); i++) {
					if (b[i] >= '0'&&b[i] <= '8') {
						int e = (int)b[i] - '0';
						e++;
						b[i] = (char)e + '0';
					}
					else if (b[i] == '9') {
						b[i] = '0';
					}
				}
			}
			if (a[i] == 'P') {
				for (int i = 0; i < b.length(); i++) {
					if (b[i] >= '1'&&b[i] <= '9') {
						int e = (int)b[i] - '0';
						e--;
						b[i] = (char)e + '0';
					}
					else if (b[i] == '0') {
						b[i] = '9';
					}
				}
			}
		}
		cout << b << endl;
	}
}