#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<functional>
#include<map>
using namespace std;

int main() {
	int a; cin >> a;
	for (int b = 0; b < a; b++) {
		string c, d; cin >> c >> d;
		for (int i = c.length() - 1; i >= 0;i--) {
			char e = c[i];
			switch (e) {
			case 'P':for (int f = 0; f < d.length(); f++) {
				if (d[f] == '0')d[f] = '9';
				else if (d[f] <= '9')d[f]--;
			} break;
			case 'M':for (int f = 0; f < d.length(); f++) {
				if (d[f] == '9')d[f] = '0';
				else if (d[f] <= '9')d[f]++;
			} break;
			case 'C':d = d.substr(1, d.length() - 1) + d[0]; break;
			case 'J':d = d[d.length() - 1] + d.substr(0, d.length() - 1); break;
			case 'A': {string f = d;
				for (int g = 0; g < d.length(); g++) {
					d[g] = f[d.length() - 1 - g];
				}
				break; }
			default: {
				if (d.length() & 1)
				{
					string f = d;
					d = ""; 
					d += f.substr(f.length() - f.length() / 2, f.length() / 2);
					d += f[f.length() / 2];
					d += f.substr(0, f.length() / 2);
				}
				else {
					string f = d;
					d = "";
					d += f.substr(f.length() - f.length() / 2, f.length() / 2);
					d += f.substr(0, f.length() / 2);
				}}
			}
		}
		cout << d << endl;
	}
}