// AOJ 1240: Unreliable Message
// 2017.10.17 bal4u@uu

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char msg[2][30]; int w;

int main()
{
	int t, k1, k2, i;
	char ord[7], *s, *p, *q;

	scanf("%d", &t);
	while (t-- > 0) {
		scanf("%s%s", ord, msg[0]);
		w = strlen(msg[0]); msg[1][w] = 0; s = ord + strlen(ord)-1;
		for (k1 = 0, k2 = 1; s >= ord; s--, k1 = k2, k2 = !k2) {
			p = msg[k1], q = msg[k2];
			if (*s == 'A') {
				q += w;	while (*p) *--q = *p++;
			} else if (*s == 'C') {
				*(q+w-1) = *p++; while (*p) *q++ = *p++;
			} else if (*s == 'E') {
				i = w/2; q += i; if (w & 1) q++;
				while(i-- > 0) *q++ = *p++;
				q = msg[k2]; if (w & 1) *(q+w/2) = *p++;
				while(*p) *q++ = *p++;
			} else if (*s == 'J') {
				*q++ = *(p+w-1); while (*p) *q++ = *p++;
				*(q-1) = 0;
			} else if (*s == 'M') {
				while (*p) {
					if (isdigit(*p)) {
						if (*p == '9') *q = '0'; else *q = (*p)+1;
					} else *q = *p;
					p++, q++;
				}
			} else {
				while (*p) {
					if (isdigit(*p)) {
						if (*p == '0') *q = '9'; else *q = (*p)-1;
					} else *q = *p;
					p++, q++;
				}
			}
		}
		puts(msg[k1]);
	}
 	return 0;
}