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<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
string str;
string mess;
int main(){
	int n;
	cin>>n;
	for(int i = 0;i <n;i++){
		cin>>str;
		cin>>mess;
		reverse(str.begin(),str.end());
		for(int j = 0;j < str.length();j++){
			//cout <<j <<" "<<mess<<endl;
			if(str[j]=='A'){
				reverse(mess.begin(),mess.end());
			}else if(str[j]=='J'){
				string str1 =mess.substr(mess.length()-1,1);
				string str2 =mess.substr(0,mess.length()-1);
				mess =str1+str2;
			}else if(str[j]=='C'){
				string str1 =mess.substr(0,1);
				string str2 =mess.substr(1,mess.length()-1);
				mess =str2+str1;
			}else if(str[j]=='E'){
				if(mess.length()%2!=0){
					string str1 =mess.substr(0,mess.length()/2);
					string str2 =mess.substr(mess.length()/2+1,mess.length()/2);
					string str3 =mess.substr(mess.length()/2,1);
					mess =str2 +str3+ str1;
				}else if(mess.length()%2==0){
					string str1 =mess.substr(0,mess.length()/2);
					string str2 =mess.substr(mess.length()/2,mess.length()/2);
					mess =str2+str1;
				}
			}else if(str[j]=='P'){
				for(int k = 0;k <mess.length();k++){
					if(isdigit(mess[k])){
						if(mess[k]>='1'){
							mess[k]--;
						}else if(mess[k]=='0'){
							mess[k]='9';
						}
					}
				}
			}else if(str[j]=='M'){
				for(int k = 0;k <mess.length();k++){
					if(isdigit(mess[k])){
						if(mess[k]<='8'){
							mess[k]++;
						}else if(mess[k]=='9'){
							mess[k]='0';
						}
					}
				}
			}
		}
		cout <<mess<<endl;
	}
	return 0;
}

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

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

string J(string str){
  rotate(str.begin(),str.end()-1,str.end());
  return str;
}

string C(string str){
  rotate(str.begin(),str.begin()+1,str.end());
  return str;
}

string E(string str){
  int n = str.size();
  string l = str.substr(0,n/2);
  string r = str.substr((n+1)/2);
  string m = (n%2 == 0 ? "" : str.substr(n/2,1));
  return r + m + l;
}

string A(string str){
  return string(str.rbegin(), str.rend());
}

string P(string str){
  FOR(it,str){
    if(isdigit(*it)){
      int num = *it - '0';
      num = (num + 10 - 1) % 10;
      *it = num + '0';
    }
  }
  return str;
}
string M(string str){
  FOR(it,str){
    if(isdigit(*it)){
      int num = *it - '0';
      num = (num + 1) % 10;
      *it = num + '0';
    }
  }
  return str;
}

int main(){
  int n;
  cin>>n;
  while(n --> 0){
    string mes;
    string str;
    cin>>mes>>str;
    for(int i=mes.size()-1; i>=0; i--){
      switch(mes[i]){
      case 'J':
        str = J(str);
        break;
      case 'C':
        str = C(str);
        break;
      case 'E':
        str = E(str);
        break;
      case 'A':
        str = A(str);
        break;
      case 'P':
        str = P(str);
        break;
      case 'M':
        str = M(str);
        break;
      }
    }
    cout << str << endl;
  }
  return 0;
}