Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	n := io.NextInt()
	for i := 0; i < n; i++ {
		s, t := io.NextLine(), io.NextLine()
		io.Println(f(s, t))
	}
}

func f(s, t string) string {
	for i := len(s) - 1; i >= 0; i-- {
		switch s[i] {
		case 'C':
			t = strings.Join([]string{t[1:], string(t[0])}, "")
		case 'J':
			t = strings.Join([]string{string(t[len(t)-1]), t[:len(t)-1]}, "")
		case 'E':
			if len(t)%2 == 1 {
				t = strings.Join([]string{t[len(t)/2+1:], string(t[len(t)/2]), t[:len(t)/2]}, "")
			} else {
				t = strings.Join([]string{t[len(t)/2:], t[:len(t)/2]}, "")
			}
		case 'A':
			r := []rune(t)
			for i, n := 0, len(r); i < n/2; i++ {
				r[i], r[n-i-1] = r[n-i-1], r[i]
			}
			t = string(r)
		case 'M':
			r := []rune(t)
			for i := 0; i < len(r); i++ {
				if '0' <= r[i] && r[i] < '9' {
					r[i]++
				} else if r[i] == '9' {
					r[i] = '0'
				}
			}
			t = string(r)
		case 'P':
			r := []rune(t)
			for i := 0; i < len(r); i++ {
				if '0' < r[i] && r[i] <= '9' {
					r[i]--
				} else if r[i] == '0' {
					r[i] = '9'
				}
			}
			t = string(r)
		}
	}
	return t
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge{int to,from,cost;};

#ifdef DEBUGLOCAL
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;i--)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define repnd(i,m,n) for(int i=(n)-1;i>=(m);i--)
#define rep0(i,n) for(i=0;i<(n);++i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,-1,1,-1,0};
const int dy[]={0,0,1,-1,1,-1,-1,1,0};

string C(string mes){
	return mes.substr(1,sz(mes)-1)+mes.substr(0,1);
}

string J(string mes){
	return mes.substr(sz(mes)-1,1)+mes.substr(0,sz(mes)-1);
}

string E(string mes){
	if(sz(mes)%2==0)return mes.substr(sz(mes)/2,sz(mes)/2)+mes.substr(0,sz(mes)/2);
	else return mes.substr(sz(mes)/2+1,sz(mes)/2)+mes.substr(sz(mes)/2,1)+mes.substr(0,sz(mes)/2);
}

string A(string mes){
	reverse(all(mes));
	return mes;
}

string M(string mes){
	rep(i,sz(mes)){
		if(isdigit(mes[i])){
			if(mes[i]=='9')mes[i]='0';
			else ++mes[i];
		}
	}
	return mes;
}

string P(string mes){
	rep(i,sz(mes)){
		if(isdigit(mes[i])){
			if(mes[i]=='0')mes[i]='9';
			else --mes[i];
		}
	}
	return mes;
}

int main() {
	int n=in();
	string mes,ord;
	while(n--){
		cin>>ord>>mes;
		repd(i,sz(ord)){
			switch(ord[i]){
			case 'J':
				mes=J(mes);
			break;
			case 'C':
				mes=C(mes);
			break;
			case 'E':
				mes=E(mes);
			break;
			case 'A':
				mes=A(mes);
			break;
			case 'P':
				mes=P(mes);
			break;
			case 'M':
				mes=M(mes);
			break;
			}
debug<<mes<<endl;
		}
		cout<<mes<<endl;
debug<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

void funcC(char *s,int length){
	char *tmp;
	tmp = new char[length];

	for(int i=0;i<length-1;i++){
		tmp[i] = s[i+1];
	}
	tmp[length-1] = s[0];

	for(int i=0;i<length;i++){
		s[i] = tmp[i];
	}
}


void funcJ(char *s,int length){
	char *tmp;
	tmp = new char[length];

	for(int i=1;i<length;i++){
		tmp[i] = s[i-1];
	}
	tmp[0] = s[length-1];

	for(int i=0;i<length;i++){
		s[i] = tmp[i];
	}
}

void funcE(char *s,int length){
	char *tmp1,*tmp2;
	int num=0;

	if(length%2==0){
		num = length/2;
	}
	else num =length/2 +1;

	tmp1 = new char[length];
	tmp2 = new char[length];

	for(int i=0;i<length/2;i++){
		tmp1[i] = s[i];
		tmp2[i] = s[num+i];
	}

	for(int i=0;i<length/2;i++){
		s[i] = tmp2[i];
	}

	for(int i=num;i<length;i++){
		s[i] = tmp1[i-num];
	}

}

void funcA(char *s,int length){
	char *tmp;
	tmp = new char[length];

	for(int i=0;i<length;i++){
		tmp[i] = s[length-1-i];
	}

	for(int i=0;i<length;i++){
		s[i]   = tmp[i];
	}
}

void funcM(char *s,int length){
	
	for(int i=0;i<length;i++){
		if(s[i] >='0' &&s[i]<='8'){
			s[i]++;
		}
		else if(s[i]=='9'){
			s[i]='0';
		}
	}
}

void funcP(char *s,int length){
	for(int i=0;i<length;i++){
		if(s[i] >='1' &&s[i]<='9'){
			s[i]--;
		}
		else if(s[i]=='0'){
			s[i]='9';
		}
	}
}


int main(){

	int num;

	cin>>num;

	for(int k=0;k<num;k++){
		string data;

		string str;
		cin>>data;
		cin>>str;

		char tmp[30];
		for(int i=0;i<str.size();i++){
			tmp[i] = str.at(i);
		}
		tmp[str.size()] = '\0';

		for(int i=data.size()-1;i>=0;i--){

			if(data.at(i)=='A'){
				funcA(tmp,str.size());
			}
			else if(data.at(i)=='J')funcJ(tmp,str.size());
			else if(data.at(i)=='M')funcM(tmp,str.size());
			else if(data.at(i)=='P')funcP(tmp,str.size());
			else if(data.at(i)=='C')funcC(tmp,str.size());
			else if(data.at(i)=='E')funcE(tmp,str.size());
		}

		cout<<tmp<<endl;

	}





	return 0;
}