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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}
template<typename T,typename U>
ll FindErase(T &v,U tar){
  ll cnt = 0;
  for(auto it = v.begin(); it != v.end();){
    if(*it == tar){
      it = v.erase(it);
      ++cnt;
    }
    else{
      ++it;
    }
  }
  return cnt;
}

template<typename T>
bool SuffixErase(T &v,size_t suf){
  if(suf > v.size()) return false;
  for(auto it = v.begin(); it != v.end();){
    if(distance(v.begin(),it) == suf){
      v.erase(it);
      return true;
    }
    else{
      ++it;
    }
  }
  return false;
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string s,s2;
  rep(i,n){
    ll k = 0;
    cin >> s;
    cin >> s2;
    k = s2.size();
    for(int j = s.size()-1; j >=0 ; --j){
      if(s[j] == 'C'){
        s2 += s2;
        s2 = s2.substr(1,k);
      }
      else if(s[j] == 'J'){
        s2 += s2;
        s2 = s2.substr(k-1,k);
      }
      else if(s[j] == 'E'){
        if(s2.size() % 2 == 0)
          rotate(s2.begin(),s2.begin()+s2.size()/2,s2.end());
        else{
          s2 = s2.substr(s2.size()/2+1) + s2[s2.size()/2] + s2.substr(0,s2.size()/2);
        }
      }
      else if(s[j] == 'A'){
        reverse(all(s2));
      }
      else if(s[j] == 'M'){
        rep(l,s2.size()){
          if(isdigit(s2[l])){
            if(s2[l] != '9'){
              s2[l]++;
            }
            else{
              s2[l] = '0';
            }
          }
        }
      }
      else if(s[j] == 'P'){
        rep(l,s2.size()){
          if(isdigit(s2[l])){
            if(s2[l] != '0'){
              s2[l]--;
            }
            else{
              s2[l] = '9';
            }
          }
        }
      }
    }
    cout << s2 << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;

void Mr_J(string *str){
  
  string s1,s2;
  
  s1=(*str).substr(0,1);
  s2=(*str).substr(1);
  
  (*str)=s2+s1;
  
}
void Miss_C(string *str){

  string s1,s2;
  
  s1=(*str).substr(0,(*str).size()-1);
  s2=(*str).substr((*str).size()-1);
  
  (*str)=s2+s1;
  
}
void Mr_E(string *str){
  
  string s1,s2,s3;
  
  if((*str).size() % 2 == 0){
    s1=(*str).substr(0,(*str).size()/2);
    s2=(*str).substr((*str).size()/2);
    (*str)=s2+s1;
  }else{
    s1=(*str).substr(0,(*str).size()/2);
    s2=(*str).substr(((*str).size()/2)+1);
    s3=(*str).substr((*str).size()/2,1);
    (*str)=s2+s3+s1;
  }
  
}
void Mr_A(string *str){
  
  string s1;
  
  s1="";
  
  for(int i=(*str).size()-1 ; i >= 0 ; i--){
    s1+=(*str)[i];
  }
  
  (*str)=s1;
  
}
void Dr_P(string *str){
  
  for(int i=0 ; i < (*str).size() ; i++){
    if((*str)[i] >= '0' && (*str)[i] <= '9'){
      (*str)[i]=(*str)[i]+1;    
      if((*str)[i] > '9')(*str)[i]='0';
    }
  }
  
}
void Mr_M(string *str){
  
  for(int i=0 ; i < (*str).size() ; i++){
    if((*str)[i] >= '0' && (*str)[i] <= '9'){
      (*str)[i]=(*str)[i]-1;
      if((*str)[i] < '0')(*str)[i]='9';
    }
  }
}
int main(){
  
  int n;
  
  cin >>n;
  
  for(int i=0 ; i < n ; i++){
    string key,str;
    cin >>key;
    cin >>str;
    for(int j=key.size()-1 ; j >= 0; j--){
      switch(key[j]){
      case 'J':
	Miss_C(&str);
	break;
      case 'C':
	Mr_J(&str);
	break;
      case 'E':
	Mr_E(&str);
	break;
      case 'A':
	Mr_A(&str);
	break;
      case 'P':
	Mr_M(&str);
	break;
      case 'M':
	Dr_P(&str);
	break;
      }
    }
    cout <<str<<endl;
  }
  
  return 0;
}