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<string>
#include<algorithm>

using namespace std;

void QueryJ(string&);
void QueryC(string&);
void QueryE(string&);
void QueryA(string&);
void QueryP(string&);
void QueryM(string&);
bool IsNumber(const char);

int main(){
  int i, j, n;
  string data, str;

  cin >> n;

  for(i=0; i<n; ++i){
    cin >> data >> str;

    for(j=data.size()-1; j>=0; --j){
      if(data[j] == 'J') QueryJ(str);
      else if(data[j] == 'C') QueryC(str);
      else if(data[j] == 'E') QueryE(str);
      else if(data[j] == 'A') QueryA(str);
      else if(data[j] == 'P') QueryP(str);
      else if(data[j] == 'M') QueryM(str);
    }

    cout << str << endl;
  }
  return 0;
}

void QueryJ(string& str){
  char c;
  c = str[str.size()-1];
  str.erase(str.end()-1);
  str.insert(str.begin(), c);
}

void QueryC(string& str){
  char c;
  c = str[0];
  str.erase(str.begin());
  str.push_back(c);
}

void QueryE(string& str){
  string s;
  if(str.size()%2 == 0){
    copy(str.begin()+str.size()/2, str.end(), back_inserter(s));
  }else{
    copy(str.begin()+str.size()/2+1, str.end(), back_inserter(s));
    s.push_back(str[str.size()/2]);
  }
  copy(str.begin(), str.begin()+str.size()/2, back_inserter(s));
  str = s;
}

void QueryA(string& str){
  int i;
  string s;
  for(i=str.size()-1; i>=0; --i){
    s.push_back(str[i]);
  }
  str = s;
}

void QueryP(string& str){
  string::iterator i;
  i = str.begin();
  while(1){
    i = find_if(i, str.end(), IsNumber);
    if(i == str.end()) break;
    if(*i == '0') *i = '9';
    else --(*i);
    ++i;
  }
}

void QueryM(string& str){
  string::iterator i;
  i = str.begin();
  while(1){
    i = find_if(i, str.end(), IsNumber);
    if(i == str.end()) break;
    if(*i == '9') *i = '0';
    else ++(*i);
    ++i;
  }
}

bool IsNumber(const char c){
  if(c >= '0' && c <= '9')
    return true;
  else
    return false;
}

C++: #include<iostream>
#include<iomanip>
#include<queue>
#include<string>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<utility>
#include<map>
#include<set>
#include<vector>
#include<functional>

using namespace std;

#ifdef DEBUG
#define dprintf      printf
#define dout(x)      cout<<#x" = "<<(x)<<endl
#define darray(x,n)    {int i;rep(i,n)cout<<#x"["<<i<<"] = "<<*((x)+i)<<endl;}
#define dloop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define drep(i,n)    for((i)=0;(i)<(n);(i)++)
#define dstop()      scanf("%*c")
#else
#define dprintf      (1)?0:printf
#define dout(x)
#define darray(x,n)
#define dloop(i,s,g) if(1){}else
#define drep(i,n)    if(1){}else
#define dstop()      if(1){}else
#endif

#define F    first
#define S    second
#define pb   push_back
#define mp   make_pair

#define loop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define rep(i,n)    for((i)=0;(i)<(n);(i)++)
#define all(x)      (x.begin(),x.end())
#define in(T,...) T __VA_ARGS__; impl(__VA_ARGS__);
#define array(T,id,n) T id[n]; rep(i,n)cin>>id[i];

//#define int long long;
#ifndef int
#define INF (0x7fffffff)
#else
#define INF (0x7fffffffffffffff)
#endif

typedef  long long           ll;
typedef  unsigned            ui;
typedef  unsigned long long  ull;
typedef  pair<int,int>       i_i;
typedef  pair<ll,int>        ll_i;
typedef  pair<ll,ll>         ll_ll;
typedef  pair<double,int>    d_i;
typedef  pair<double,double> d_d;

void impl(){};
template <typename T,typename... TS >
void impl(T &head,TS &... tail)
{
  cin>>head;
  impl(tail ...);
}

void dej(char *str)
{
  int len=strlen(str);
  char a=str[len-1];
  str[len-1]=0;
  char ptr[112345]={};
  sprintf(ptr,"%c%s",a,str);
  sprintf(str,"%s",ptr);
}
void dec(char *str)
{
  sprintf(str,"%s%c",&str[1],str[0]);
}
void dee(char *str)
{
  int len=strlen(str);
  char top[112345]={};sprintf(top,"%s",str);top[len/2]=0;
  char mid[10]={};if(len%2)mid[0]=str[(len+1)/2-1];
  char bot[112345]={};sprintf(bot,"%s",&str[(len+1)/2]);
  sprintf(str,"%s%s%s",bot,mid,top);
}
void dea(char *str)
{
  int i;
  int len=strlen(str)-1;
  int n=(len+1)/2;
  char t;
  rep(i,n)
    {
      t=str[i];
      str[i]=str[len-i];
      str[len-i]=t;
    }
}
void dep(char *str)
{
  int i;
  int n=strlen(str);
  rep(i,n)
    if('0'<=str[i] && str[i]<='9')
      {
	if(str[i]=='0')
	  str[i]='9';
	else
	  str[i]--;
      }
}
void dem(char *str)
{
  int i;
  int n=strlen(str);
  rep(i,n)
    if('0'<=str[i] && str[i]<='9')
      {
	if(str[i]=='9')
	  str[i]='0';
	else
	  str[i]++;
      }
}

void decode(char *str,char type)
{
  switch(type)
    {
    case 'J':dej(str);break;
    case 'C':dec(str);break;
    case 'E':dee(str);break;
    case 'A':dea(str);break;
    case 'P':dep(str);break;
    case 'M':dem(str);break;
    }
}

signed main(void)
{
  char str[112345]={};
  char type[112345]={};
  int n;
  cin>>n;
  int i;
  int j,len;
  rep(i,n)
    {
      scanf("%s",type);
      scanf("%s",str);
      len=strlen(type);
      rep(j,len)
	decode(str,type[len-j-1]);
      printf("%s\n",str);
    } 
  return 0;
}