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)
}

Python: """
J氏は、メッセージのすべての文字を左に1つ回転させます。
たとえば、「aB23d」を「B23da」に変換します。

ミスCは、メッセージのすべての文字を1つ右に回転します。
たとえば、彼女は「aB23d」を「daB23」に変換します。

E氏はメッセージの左半分を右半分と入れ替えます。メッセージの文字数が奇数の場合、中央の文字は移動しません。
たとえば、「e3ac」を「ace3」に、「aB23d」を「3d2aB」に変換します。

A氏はメッセージを逆にします。たとえば、「aB23d」を「d32Ba」に変換します。

Dr. Pは、メッセージ内のすべての数字を1つ増やします。数字が「9」の場合、「0」になります。アルファベットは変更されません。
たとえば、彼は「aB23d」を「aB34d」に、「e9ac」を「e0ac」に変換します。

M氏は、メッセージのすべての桁を1つ減らします。数字が「0」の場合、「9」になります。アルファベットは変更されません。
たとえば、彼は「aB23d」を「aB12d」に、「e0ac」を「e9ac」に変換します。
"""

def func_by_J(t):
    return t[len(t)-1] + t[:len(t)-1]

def func_by_C(t):
    return t[1:len(t)] + t[0]

def func_by_E(t):
    if len(t)%2 == 0:
        t = t[len(t)//2:] + t[:len(t)//2]
    else:
        t = t[len(t)//2+1:] + t[len(t)//2] + t[:len(t)//2]
    return t

def func_by_A(t):
    return t[::-1]

def func_by_P(t):
    s = []
    for c in t:
        try:
            integer = int(c)
            s.append(str((integer - 1)%10))
        except:
            s.append(c)

    return "".join(s)

def func_by_M(t):
    s = []
    for c in t:
        try:
            integer = int(c)
            s.append(str((integer + 1)%10))
        except:
            s.append(c)

    return "".join(s)


def solve(s, t):
    for c in reversed(s):
        if c == "J":
            t = func_by_J(t)
        elif c == "C":
            t = func_by_C(t)
        elif c == "E":
            t = func_by_E(t)
        elif c == "A":
            t = func_by_A(t)
        elif c == "P":
            t = func_by_P(t)
        elif c == "M":
            t = func_by_M(t)
    return t


if __name__ == '__main__':
    #"""
    n = int(input())
    ans = []
    for i in range(n):
        s = input()
        t = input()
        ans.append(solve(s, t))
    print(*ans, sep="\n")

    """
    t = "abcd019"
    print(t)
    print(func_by_J(t))
    print(func_by_C(t))
    print(func_by_E(t))
    print(func_by_A(t))
    print(func_by_P(t))
    print(func_by_M(t))
    """

Python: #!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    for i in range(n):
        s = input()
        t = S()
        for k in s[::-1]:
            if k == "J":
                t = [t[-1]]+t[:-1]
            elif k == "C":
                t = t[1:]+[t[0]]
            elif k == "E":
                m = len(t) >> 1
                if len(t)&1:
                    t = t[m+1:]+[t[m]]+t[:m]
                else:
                    t = t[m:]+t[:m]
            elif k == "A":
                t = t[::-1]
            elif k == "M":
                for j in range(len(t)):
                    if t[j].isdecimal():
                        t[j] = str(int(t[j])+1)[-1]
            else:
                for j in range(len(t)):
                    if t[j].isdecimal():
                        if t[j] == "0":
                            t[j] = "9"
                        else:
                            t[j] = str(int(t[j])-1)
        print(*t,sep = "")
    return

#Solve
if __name__ == "__main__":
    solve()

Python: def decode(mes,command):
    if command == 'J':
        return mes[-1] + mes[:-1]
    elif command == 'C':
        return mes[1:] + mes[0]
    elif command == 'E':
        m = len(mes)//2
        if len(mes)%2 == 0:
            return mes[m:] + mes[:m]
        else:
            return mes[m+1:] + mes[m] + mes[:m]
    elif command == 'A':
        return mes[::-1]
    else:
        d = (-1 if command == 'P' else 1)
        ret = ''
        for c in mes:
            if c.isdigit():
                ret += str((int(c)+d) % 10)
            else:
                ret += c
        return ret

N = int(input())
for i in range(N):
    order,mes = input(),input()
    for c in reversed(order):
        mes = decode(mes,c)
    print(mes)

Python: 
n = int(input())

for i in range(n):
  order = input()
  message = input()

  for s in order:
    if s == 'J':
      message = message[1:] + message[0]
    elif s == 'C':
      message = message[len(message) - 1] + message[:len(message) - 1]
    elif s == 'E':
      message = message[len(message)//2:] + message[:len(message)//2] 
    elif s == 'A':
      message = message[::-1]
    elif s == 'P':
      for c in message:
        if c == '9':
          c = '0'
        elif c >= '0' and c < '9':
          c = chr(ord(c) + 1)
    elif s == 'M':
      for c in message:
        if c == '0':
          c = '9'
        elif c > '0' and c <= '9':
          c = chr(ord(c) - 1)
    
  print(message)