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 <utility>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
#define _OP(t) inline operator t()
struct _in {
#ifdef _GLIBCXX_STRING
	_OP(string){string s;for(char c;c=gcu(),c!=' '&&c!='\n';)s+=c;return s;}
#endif
	_OP(char){char c=gcu();gcu();return c;}
	_OP(double){double d; scanf("%lf",&d); gcu();return d;}
	_T _OP(T){T n{},m{1},c;if((c=gcu())=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
} in;
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &o) {int c{gcu()};return c==EOF?false:ungetc(c,stdin),o=in,true;}
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{
	int e,b=0,s=1; range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it { int v, s; it (int _v, int _s) : v(_v), s(_s) {} operator int()const{return v;} operator int&(){return v;} int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin() {return {b, s};} it end() {return {e, s};}};

int main() {
	for (int n(in); n; n--) {
		string k = move(in), s = move(in);
		for (auto i {rbegin(k)}; i != rend(k); i++)
			switch(*i) {
			case 'C':
				s = s.substr(1, s.size()) + s.front();
				break;
			case 'J':
				s = s.back() + s.substr(0, s.size() - 1);
				break;
			case 'E':
				for (int j: range(s.size() / 2))
					swap(s[j], s[j + (s.size() + 1) / 2]);
				break;
			case 'P':
				for (char &j: s)
					if (isdigit(j))
						j = (j - '0' + 9) % 10 + '0';
				break;
			case 'M':
				for (char &j: s)
					if (isdigit(j))
						j = (j - '0' + 1) % 10 + '0';
				break;
			case 'A':
				reverse(begin(s), end(s));
				break;
			}
		outl(s);
	}
}

C++: #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i=a; i<n; i++)
#define RREP(i, a, n) for(ll i=n-1; i>=a; i--)
typedef long long ll;
const ll mod =1e9+7;
const ll inf =1e18;
using namespace std;

ll n;
int main() {
    cin>>n;

    REP(i, 0, n){
        string order_mes, king_mes;
        cin>>order_mes>>king_mes;

        ll len=king_mes.size(); // size of king_mes
        RREP(j, 0, order_mes.size()){
            if(order_mes[j] == 'C'){
                king_mes = king_mes.substr(1,len-1)+king_mes.substr(0, 1);
            }
            else if(order_mes[j] == 'J'){
                king_mes = king_mes.substr(len-1, 1)+king_mes.substr(0, len-1);
            }
            else if(order_mes[j] == 'E'){
                if(len%2 == 0){
                    king_mes = king_mes.substr(len/2, len/2)+king_mes.substr(0, len/2);
                }
                else{
                    king_mes = king_mes.substr(len/2+1, len/2)+king_mes.substr(len/2, 1)+king_mes.substr(0, len/2);
                }
            }
            else if(order_mes[j] == 'A'){
                reverse(king_mes.begin(), king_mes.end());
            }
            else if(order_mes[j] == 'M'){
                REP(k, 0, len){
                    if('0'<=king_mes[k] && king_mes[k]<'9'){
                        king_mes[k] = king_mes[k]+1;
                    }
                    else if(king_mes[k]=='9'){
                        king_mes[k]='0';
                    }
                }
            }
            else{
                REP(k, 0, len){
                    if('0'<king_mes[k] && king_mes[k]<='9'){
                        king_mes[k] = king_mes[k]-1 ;
                    }
                    else if(king_mes[k]=='0'){
                        king_mes[k]='9';
                    }
                }
            }
        }

        cout<<king_mes<<endl;
    }
    return 0;
}