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: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    n = I()
    ni = 0
    d = '0123456789'

    while ni < n:
        ni += 1
        s = S()
        m = S()
        l = len(m)
        for c in s[::-1]:
            if c == 'J':
                m = m[-1] + m[:-1]
            elif c == 'C':
                m = m[1:] + m[0]
            elif c == 'E':
                if l % 2 == 0:
                    m = m[l//2:] + m[:l//2]
                else:
                    m = m[l//2+1:] + m[l//2] + m[:l//2]
            elif c == 'A':
                m = m[::-1]
            elif c == 'P':
                m = ''.join([t if not t in d else d[d.index(t)-1] for t in m])
            elif c == 'M':
                m = ''.join([t if not t in d else d[(d.index(t)+1)%10] for t in m])

        rr.append(m)

    return '\n'.join(map(str, rr))


print(main())

Python: def rotateRight(msg):
    if len(msg) == 1:
        return msg
    return msg[-1] + msg[:-1]

def rotateLeft(msg):
    if len(msg) == 1:
        return msg
    return msg[1:] + msg[0]

def swapHalf(msg):
    if len(msg) == 1:
        return msg
    H = len(msg) // 2
    if len(msg) % 2 == 0:
        return msg[H:] + msg[:H]
    else:
        return msg[H+1:] + msg[H] + msg[:H]

def increment(msg):
    arr = [ c for c in msg ]
    for i in range(len(arr)):
        if arr[i].isdigit():
            arr[i] = str((int(arr[i]) + 1) % 10)
    return "".join(arr)

def decrement(msg):
    arr = [ c for c in msg ]
    for i in range(len(arr)):
        if arr[i].isdigit():
            if arr[i] == '0':
                arr[i] = '9'
            else:
                arr[i] = str(int(arr[i]) - 1)
    return "".join(arr)

if __name__ == '__main__':
    N = int(input())

    for _ in range(N):
        order = input()
        msg = input()

        for i in range(len(order) - 1, -1, -1):
            c = order[i]
            if c == 'J':
                msg = rotateRight(msg)
            elif c == 'C':
                msg = rotateLeft(msg)
            elif c == 'E':
                msg = swapHalf(msg)
            elif c == 'A':
                msg = msg[::-1]
            elif c == 'P':
                msg = decrement(msg)
            elif c == 'M':
                msg = increment(msg)
        
        print(msg)

Python: def C(S):
    return S[1:] + S[0]


def J(S):
    return S[-1] + S[:-1]


def E(S):
    temp = len(S) // 2
    if len(S) % 2 == 0:
        S = S[temp:] + S[:temp]
    else:
        S = S[temp + 1:] + S[temp] + S[:temp]

    return S


def A(S):
    return S[::-1]


def M(S):
    temp = []
    for s in S:
        if s.isdecimal():
            temp.append("0" if s == "9" else str(int(s) + 1))
        else:
            temp.append(s)
    return "".join(temp)


def P(S):
    temp = []
    for s in S:
        if s.isdecimal():
            temp.append("9" if s == "0" else str(int(s) - 1))
        else:
            temp.append(s)
    return "".join(temp)


ans = []

n = int(input())

for i in range(n):
    messenger = input()
    messenger = messenger[::-1]
    S = input()
    for m in messenger:
        if m == "J":
            S = J(S)
        elif m == "C":
            S = C(S)
        elif m == "E":
            S = E(S)
        elif m == "A":
            S = A(S)
        elif m == "P":
            S = P(S)
        elif m == "M":
            S = M(S)

    ans.append(S)

for s in ans:
    print(s)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

from math import *

functions = dict()
functions['J'] = lambda s: s[1:]+s[0]
functions['C'] = lambda s: s[-1] + s[:-1]
functions['A'] = lambda s: ''.join(reversed(s))
functions['E'] = lambda s: s[ceil(len(s)/2):] + s[len(s)//2:ceil(len(s)/2)] + s[:len(s)//2]
functions['P'] = lambda s: ''.join([str((int(c)+1)%10) if c.isnumeric() else c for c in s])
functions['M'] = lambda s: ''.join([str((int(c)-1)%10) if c.isnumeric() else c for c in s])

inv = {'J':'C','C':'J','A':'A','E':'E','P':'M','M':'P'}

n = int(input())
for i in range(n):
    order = input()
    message = str(input())
    for o in list(reversed(order)):
        message = functions[inv[o]](message)
    print(message)