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<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double PI = 3.14159265358979;
const double E = 2.718281828459045;
const double root2 = sqrt(2);
typedef pair<int, int> P;
typedef unsigned int ui;
string s;
void traJ() {
	int len = s.length();
	char memo = s[len - 1];
	for (int i = len-1; i > 0; i--) {
		s[i] = s[i - 1];
	}
	s[0] = memo;
}
void traC() {
	int len = s.length();
	char memo = s[0];
	for (int i = 0; i < len-1; i++) {
		s[i] = s[i + 1];
	}
	s[len-1] = memo;
}
void traE() {
	int len = s.length();
	string sle = s.substr(0, len / 2);
	string sri = s.substr((len + 1) / 2, len - (len + 1) / 2);
	for (int i = 0; i < len - (len + 1) / 2; i++) {
		s[i] = sri[i];
	}
	for (int i = (len + 1) / 2; i < len; i++) {
		s[i] = sle[i - (len + 1) / 2];
	}
}
void traA() {
	reverse(s.begin(), s.end());
}
void traP() {
	int len = s.length();
	for (int i = 0; i < len; i++) {
		if ('1' <= s[i] && s[i] <= '9') {
			s[i]--;
		}
		else if (s[i] == '0') {
			s[i] = '9';
		}
	}
}
void traM() {
	int len = s.length();
	for (int i = 0; i < len; i++) {
		if ('0' <= s[i] && s[i] <= '8') {
			s[i]++;
		}
		else if (s[i] == '9') {
			s[i] = '0';
		}
	}
}
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string ord;
		cin >> ord;
		cin >> s;
		int l = ord.length();
		for (int i = l - 1; i >= 0; i--) {
			if (ord[i] == 'J')traJ();
			if (ord[i] == 'C')traC();
			if (ord[i] == 'E')traE();
			if (ord[i] == 'A')traA();
			if (ord[i] == 'P')traP();
			if (ord[i] == 'M')traM();
		}
		cout << s << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
double EPS=1e-10;
int INF=1e9;
string C(string s){
      string res=s;
      for(int i=1;i<s.size();i++){
            res[i-1]=s[i];
      }
      res[s.size()-1]=s[0];
      return res;
}
string J(string s){
      string res=s;
      res[0]=s[s.size()-1];
      for(int i=0;i<s.size()-1;i++){
            res[i+1]=s[i];
      }
      return res;
}
string E(string s){
      int sl=s.size();
      char t;
      string tmp1="",tmp2="",res="";
      REP(i,sl){
            if(i<sl/2)tmp1+=s[i];
            else if(sl%2==1&&i==sl/2){
                  t=s[i];
            }else{
                  tmp2+=s[i];
            }
      }
      if(sl%2==1){
            return tmp2+t+tmp1;
      }
      return tmp2+tmp1;
}
string A(string s){
      reverse(ALL(s));
      return s;
}
string M(string s){
      REP(i,s.size()){
            if('0'<=s[i]&&s[i]<='9'){
                  s[i]=(s[i]-'0'+1)%10+'0';
            }
      }
      return s;
}
string P(string s){
      REP(i,s.size()){
            if('0'<=s[i]&&s[i]<='9'){
                  s[i]=(s[i]-'0'+9)%10+'0';
            }
      }
      return s;
}
int main(){
      string s,t;
      int n;
      cin>>n;
      while(n--){
            cin>>t>>s;
            for(int i=t.size()-1;i>=0;i--){
                  if(t[i]=='J')s=J(s);
                  else if(t[i]=='C')s=C(s);
                  else if(t[i]=='E')s=E(s);
                  else if(t[i]=='A')s=A(s);
                  else if(t[i]=='P')s=P(s);
                  else s=M(s);
            }
            cout<<s<<endl;
      }
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>

using namespace std;


bool solve(){
	string str, ans;
	cin>> str>> ans;
	
	for(int i=0;i<str.size();i++){
		int n = ans.size();
		char c;
		string lstr, rstr;

		switch(str[str.size()-1-i]){
		case 'J':
			c = ans[n-1];
			for(int j=n-1;j>=0;j--) ans[j] = ans[(j+n-1)%n];
			ans[0] = c;
			break;
		case 'C':
			c = ans[0];
			for(int j=0;j<n;j++) ans[j] = ans[(j+1)%n];
			ans[n-1] = c;
			break;
		case 'E':
			if(n%2==1){
				lstr = ans.substr(0,(n-1)/2);
				rstr = ans.substr((n+1)/2);
				rstr += ans[(n-1)/2];
			}else{
				lstr = ans.substr(0,n/2);
				rstr = ans.substr(n/2);
			}
			ans = rstr + lstr;
			break;
		case 'A':
			reverse(ans.begin(), ans.end());
			break;
		case 'P':
			for(int j=0;j<n;j++){
				if('0'<=ans[j] && ans[j]<='9'){
					ans[j] = '0' + ((ans[j]-'0') + 9)%10;
				}
			}
			break;
		case 'M':
			for(int j=0;j<n;j++){
				if('0'<=ans[j] && ans[j]<='9'){
					ans[j] = '0' + ((ans[j]-'0') + 1)%10;
				}
			}
			break;
		default:
			break;
		}
	}

	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	int n;
	cin>> n;
	for(int i=0;i<n;i++) solve();

	return 0;
}