import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;

void main()
{
	auto n = readln.chomp.to!int;
	while (n--) {
		auto pat = readln.chomp;
		auto str = readln.chomp;
		foreach_reverse (e; pat) {
			if (e == 'C') {
				str = str[1..$] ~ str[0];
			} else if (e == 'J') {
				str = str[$-1] ~ str[0..$-1];
			} else if (e == 'E') {
				if (str.length % 2) {
					str = str[str.length/2+1..$] ~ str[str.length/2] ~ str[0..str.length/2];
				} else {
					str = str[str.length/2..$] ~ str[0..str.length/2];
				}
			} else if (e == 'A') {
				str = str.array.reverse.to!string;
			} else if (e == 'M') {
				foreach (i; 0..str.length) {
					if (str[i] >= '0' && str[i] < '9') {
						str = str[0..i] ~ (str[i] + 1).to!char ~ str[i+1..$];
					} else if (str[i] == '9') {
						str = str[0..i] ~ '0' ~ str[i+1..$];
					}
				}
			} else {
				foreach (i; 0..str.length) {
					if (str[i] > '0' && str[i] <= '9') {
						str = str[0..i] ~ (str[i] - 1).to!char ~ str[i+1..$];
					} else if (str[i] == '0') {
						str = str[0..i] ~ '9' ~ str[i+1..$];
					}
				}
			}
		}
		str.writeln;
	}
}