Problem:
Problem A: Unreliable Messengers The King of a little Kingdom on a little island in the Pacific Ocean frequently has childish
ideas. One day he said, “You shall make use of a message relaying game when you inform me
of something.” In response to the King’s statement, six servants were selected as messengers
whose names were Mr. J, Miss C, Mr. E, Mr. A, Dr. P, and Mr. M. They had to relay a message
to the next messenger until the message got to the King. Messages addressed to the King consist of digits (‘0’-‘9’) and alphabet characters (‘a’-‘z’, ‘A’-‘Z’). Capital and small letters are distinguished in messages. For example, “ke3E9Aa” is a
message. Contrary to King’s expectations, he always received wrong messages, because each messenger
changed messages a bit before passing them to the next messenger. Since it irritated the King, he
told you who are the Minister of the Science and Technology Agency of the Kingdom, “We don’t
want such a wrong message any more. You shall develop software to correct it!” In response to
the King’s new statement, you analyzed the messengers’ mistakes with all technologies in the
Kingdom, and acquired the following features of mistakes of each messenger. A surprising point
was that each messenger made the same mistake whenever relaying a message. The following
facts were observed. Mr. J rotates all characters of the message to the left by one. For example, he transforms
“aB23d” to “B23da”. Miss C rotates all characters of the message to the right by one. For example, she transforms
“aB23d” to “daB23”. Mr. E swaps the left half of the message with the right half. If the message has an odd number
of characters, the middle one does not move. For example, he transforms “e3ac” to “ace3”,
and “aB23d” to “3d2aB”. Mr. A reverses the message. For example, he transforms “aB23d” to “d32Ba”. Dr. P increments by one all the digits in the message. If a digit is ‘9’, it becomes ‘0’. The
alphabet characters do not change. For example, he transforms “aB23d” to “aB34d”, and “e9ac”
to “e0ac”. Mr. M decrements by one all the digits in the message. If a digit is ‘0’, it becomes ‘9’. The
alphabet characters do not change. For example, he transforms “aB23d” to “aB12d”, and “e0ac”
to “e9ac”. The software you must develop is to infer the original message from the final message, given the
order of the messengers. For example, if the order of the messengers is A -> J -> M -> P and the
message given to the King is “aB23d”, what is the original message? According to the features
of the messengers’ mistakes, the sequence leading to the final message is A           J           M           P
“32Bad” --> “daB23” --> “aB23d” --> “aB12d” --> “aB23d”. As a result, the original message should be “32Bad”. Input The input format is as follows. n
     The order of messengers
     The message given to the King
                .
                .
                .
     The order of messengers
     The message given to the King The first line of the input contains a positive integer n, which denotes the number of data sets.
Each data set is a pair of the order of messengers and the message given to the King. The
number of messengers relaying a message is between 1 and 6 inclusive. The same person may
not appear more than once in the order of messengers. The length of a message is between 1
and 25 inclusive. Output The inferred messages are printed each on a separate line. Sample Input 5
AJMP
aB23d
E
86AE
AM
6
JPEM
WaEaETC302Q
CP
rTurnAGundam1isdefferentf Output for the Sample Input 32Bad
AE86
7
EC302QTWaEa
TurnAGundam0isdefferentfr

Solution (Python):
funcs = {
    'J': lambda s: s[-1:]+s[:-1],
    'C': lambda s: s[1:]+s[0],
    'E': lambda s: s[(len(s)+1)//2:]+s[len(s)//2:(len(s)+1)//2]+s[:len(s)//2],
    'A': lambda s: s[::-1],
    'P': lambda s: s.translate(str.maketrans('0123456789', '9012345678')),
    'M': lambda s: s.translate(str.maketrans('0123456789', '1234567890')),
}
n = int(input())
for i in range(n):
    s, t = [input().strip() for j in '01']
    for c in s[::-1]:
        t = funcs[c](t)
    else:
        print(t)

Solution (Python):



import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        n = input()
        for i in range(n):
            o = raw_input()
            m = raw_input()
            for c in reversed(o):
                if c == 'J':
                    m = m[-1] + m[:-1]
                elif c == 'C':
                    m = m[1:] + m[0]
                elif c == 'E':
                    if len(m) % 2 == 0:
                        m = m[len(m)/2:] + m[:len(m)/2]
                    else:
                        m = m[len(m)/2+1:] + m[len(m)/2] + m[:len(m)/2]
                elif c == 'A':
                    m = m[::-1]
                elif c == 'P':
                    tmp = ''
                    for j in range(len(m)):
                        if m[j].isdigit():
                            d = int(m[j]) - 1
                            if d < 0:
                                d += 10
                            tmp += str(d)
                        else:
                            tmp += m[j]
                    m = tmp
                elif c == 'M':
                    tmp = ''
                    for j in range(len(m)):
                        if m[j].isdigit():
                            d = int(m[j]) + 1
                            if d > 9:
                                d -= 10
                            tmp += str(d)
                        else:
                            tmp += m[j]
                    m = tmp
            print m
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Solution (Python):
for loop in range(int(raw_input())):
    manu = raw_input()
    msg = raw_input()
    size = len(msg)
    
    for s in manu[::-1]:
        if   s == "J":
            msg = msg[-1]+msg[:-1] if size > 1 else msg
        elif s == "C":
            msg = msg[1:]+msg[0] if size > 1 else msg
        elif s == "E":
            if size == 1: continue
            if size%2 == 0:
                msg = msg[size/2:]+msg[:size/2]
            else:
                msg = msg[size/2+1:]+msg[size/2]+msg[:size/2]
        elif s == "A":
            msg = msg[::-1]
        elif s == "P":
            for i in range(len(msg)):
                if msg[i].isdigit():
                    msg = msg[:i]+str((int(msg[i])-1+10)%10)+msg[i+1:]
        elif s == "M":
            for i in range(len(msg)):
                if msg[i].isdigit():
                    msg = msg[:i]+str((int(msg[i])+1)%10)+msg[i+1:]
    
    print msg

Solution (Python):



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)

Solution (Python):
def correct(m, s):
	if m == "J":
		return s[-1] + s[:len(s)-1]
	elif m == "C":
		return s[1:] + s[0]
	elif m == "E":
		length = len(s)
		if length % 2 == 0:
			return s[length / 2:] + s[:length / 2]
		else :
			return s[(length+1) / 2:] + s[(length-1) / 2] + s[:(length-1) / 2]
	elif m == "A":
		return s[::-1]
	elif m == "P":
		cor = ""
		for si in s:
			if si.isdigit():
				cor += str(9 if int(si) == 0 else int(si) - 1)
			else :
				cor += si
		return cor
	else :
		cor = ""
		for si in s:
			if si.isdigit():
				cor += str(0 if int(si) == 9 else int(si) + 1)
			else :
				cor += si
		return cor

n = int(raw_input())
for i in range(n):
	messengers = raw_input()
	order_correct = messengers[::-1]
	cor = raw_input()
	for m in order_correct:
		cor = correct(m, cor)
	print cor

Solution (Python):



n = int(raw_input())
for i in range(n):
    ger = raw_input()
    mes = raw_input()
    
    for j in range(len(ger)-1, -1, -1):
        if ger[j] == 'J':
            mes = mes[len(mes)-1]+mes[0:len(mes)-1]
        elif ger[j] == 'C':
            mes = mes[1:len(mes)]+mes[0]
        elif ger[j] == 'E':
            if len(mes)%2 == 0:
                mes = mes[len(mes)/2:]+mes[0:len(mes)/2]
            else:
                mes = mes[len(mes)/2+1:]+mes[len(mes)/2]+mes[0:len(mes)/2]
        elif ger[j] == 'A':
            mes = mes[::-1]
        elif ger[j] == 'P':
            for k in range(len(mes)):
                if '1' <= mes[k] <= '9':
                    mes = mes[:k]+chr(ord(mes[k])-1)+mes[k+1:]
                    
                elif mes[k] == '0':
                    mes = mes[:k]+'9'+mes[k+1:]
        elif ger[j] == 'M':
            for k in range(len(mes)):
                if '0' <= mes[k] <= '8':
                    mes = mes[:k]+chr(ord(mes[k])+1)+mes[k+1:]
                    
                elif mes[k] == '9':
                    mes = mes[:k]+'0'+mes[k+1:]
    print mes

Solution (Python):
def decodeJ(s):
    return s[-1] + s[:-1]
def decodeC(s):
    return s[1:] + s[0]
def decodeE(s):
    lenS = len(s)
    return s[lenS//2:] + s[:lenS//2] if lenS % 2 == 0 else s[lenS//2+1:] + s[lenS//2] + s[:lenS//2]
def decodeA(s):
    return s[::-1]
def decodeP(s):
    ret = []
    for i in s:
        try:
            ret.append(str((int(i)-1)%10))
        except:
            ret.append(i)
    return ''.join(ret)
def decodeM(s):
    ret = []
    for i in s:
        try:
            ret.append(str((int(i)+1)%10))
        except:
            ret.append(i)
    return ''.join(ret)

N = int(input())
decode = {'J': decodeJ, 'C': decodeC, 'E': decodeE, 'A': decodeA, 'P': decodeP, 'M': decodeM}
for _ in range(N):
    messengers = input()
    message = input()
    for m in messengers[::-1]:
        message = decode[m](message)
    print(message)

Solution (Python):

n = int(input())

for i in range(n):
  order = input()[::-1]
  message = input()

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

Solution (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)

Solution (Python):
n = int(input())
for i in range(n):
    order = str(input())
    mes = str(input())
    l = len(mes)

    for p in reversed(order):
        if p == "J":
            mes = mes[-1] + mes[:-1]

        elif p == "C":
            mes = mes[1:] + mes[0]

        elif p == "E":
            if l % 2 == 0:
                mes = mes[l // 2:] + mes[:l // 2]
            else:
                mes = mes[(l + 1) // 2:] + mes[(l - 1) // 2] + mes[:(l - 1) // 2]

        elif p == "A":
            mes = mes[::-1]

        elif  p == "P":
            mes_li = list(mes)
            for itr, c in enumerate(mes_li):
                if c.isdigit():
                    mes_li[itr] = str((int(c) + 9) % 10)
            mes = "".join(mes_li)

        elif p == "M":
            mes_li = list(mes)
            for itr, c in enumerate(mes_li):
                if c.isdigit():
                    mes_li[itr] = str((int(c) + 1) % 10)
            mes = "".join(mes_li)

    print(mes)

Solution (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())

Solution (Python):
import sys
import bisect

n = int(sys.stdin.readline())
for z in xrange(n):
    order = sys.stdin.readline().strip()
    string = sys.stdin.readline().strip()
    
    half = len(string) / 2
    num = set(map(str, [0,1,2,3,4,5,6,7,8,9]))
    for o in order[::-1]:

        if o == 'J':
            string = string[-1:] + string[:-1]
        elif o == 'C':
            string = string[1:] + string[:1]
        elif o == 'E':
            string = string[-half:] + string[:half] if half * 2 == len(string) or half == 0 else string[-half:] + string[half] + string[:half]
        elif o == 'A':
            string = string[::-1]
        elif o == 'P':
            temp = list(string)
            for i in xrange(len(temp)):
                if temp[i] in num:
                    temp[i] = str(int(temp[i]) + 10 - 1)[-1:]
            string = ''.join(temp)
        elif o == 'M':
            temp = list(string)
            for i in xrange(len(temp)):
                if temp[i] in num:
                    temp[i] = str(int(temp[i]) + 1)[-1:]
            string = ''.join(temp)
    print string

Solution (Python):
def P(m):
	if m.isdigit():
		next = int(m) - 1
		if next < 0: return "9"
		return str(next)
	return m
def M(m):
	if m.isdigit():
		return str((int(m) + 1) % 10)
	return m
n = input()
for i in xrange(n):
	order = raw_input()
	msg = raw_input()
	for c in reversed(order):
		if c == "J":
			msg = msg[-1] + msg[:-1]
		if c == "C":
			msg = msg[1:] + msg[0]
		if c == "E":
			l = len(msg) / 2
			if len(msg) % 2 == 1:
				msg = msg[l + 1:] + msg[l] + msg[:l]
			else:
				msg = msg[l:] + msg[:l]
		if c == "A":
			msg = msg[::-1]
		if c == "P":
			msg = "".join(P(m) for m in msg)
		if c == "M":
			msg = "".join(M(m) for m in msg)
	print msg

Solution (Python):
def C(m):
    return m[1:]+m[:1]

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

def E(m):
    lenm=len(m)
    h=lenm/2
    if h*2==lenm or h==0:
        temp=m[-h:]+m[:h]
    else:
        temp=m[-h:]+m[h]+m[:h]
    return temp

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

def M(m):
    m=list(m)
    for i in range(len(m)):
        if m[i].isdigit():
            if m[i]=="9":
                m[i]="0"
            else:
                m[i]=str(int(m[i])+1)
    return "".join(m)

def P(m):
    m=list(m)
    for i in range(len(m)):
        if m[i].isdigit():
            if m[i]=="0":
                m[i]="9"
            else:
                m[i]=str(int(m[i])-1)
    return "".join(m)

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

Solution (Python):
def J(m) :
    tmp = m[-1]
    del m[-1]
    m.insert(0, tmp)
    return m
    
def C(m) :
    tmp = m[0]
    del m[0]
    m.append(tmp)
    return m
    
def E(m) :
    if len(m) % 2 == 1 :
        x = len(m) // 2
        tmp = m[0:x]
        middle = m[x]
        del m[0:x+1]
        m = m + [middle] + tmp
        return m
    
    else :
        x = len(m) // 2
        tmp = m[0:x]
        del m[0:x]
        m = m + tmp
        return m
    
def A(m) :
    m.reverse()
    return m
    
def M(m) :
    for i in range(len(m)) :
        if '0' <= m[i] <= '8' :
            m[i] = str(int(m[i]) + 1)
        elif m[i] == '9' :
            m[i] = '0'
    return m
    
def P(m) :
    for i in range(len(m)) :
        if '1' <= m[i] <= '9' :
            m[i] = str(int(m[i]) - 1)
        elif m[i] == '0' :
            m[i] = '9'
    return m

n = int(input())
for i in range(n) :
    member = list(input())
    member.reverse()
    message = list(input())
    
    for j in range(len(member)) :
        if member[j] == 'J' :
            message = J(message)
        elif member[j] == 'C' :
            message = C(message)
        elif member[j] == 'E' :
            message = E(message)
        elif member[j] == 'A' :
            message = A(message)
        elif member[j] == 'P' :
            message = P(message)
        elif member[j] == 'M' :
            message = M(message)
    print(*message, sep='')

Solution (Python):
funcs = {
    'J': lambda s: s[-1:]+s[:-1],
    'C': lambda s: s[1:]+s[0],
    'E': lambda s: s[(len(s)+1)//2:]+s[len(s)//2:(len(s)+1)//2]+s[:len(s)//2],
    'A': lambda s: s[::-1],
    'P': lambda s: s.translate(str.maketrans('0123456789', '9012345678')),
    'M': lambda s: s.translate(str.maketrans('0123456789', '1234567890')),
}
n = int(input())
for i in range(n):
    s, t = [input().strip() for j in '01']
    for c in s[::-1]:
        t = funcs[c](t)
    else:
        print(t)

Solution (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))
    """

Solution (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)

Solution (Python):
def J(string):
    return string[-1] + string[:-1] if len(string) >= 2 else string
     
def C(string):
    return string[1:] + string[0] if len(string) >= 2 else string

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

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

def M(string):
    new = ''
    for i in string:
        if ord(i) < 57 and ord(i) >= 48:
            new += chr(ord(i) + 1)
        elif ord(i) == 57:
            new += '0'
        else:
            new += i
    return new

def P(string):
    new = ''
    for i in string:
        if ord(i) <= 57 and ord(i) > 48:
            new += chr(ord(i) - 1)
        elif ord(i) == 48:
            new += '9'
        else:
            new += i
    return new


ans = []
for i in range(int(input())):
    to_do = input()
    message = input().strip()
    
    for i in to_do[::-1]:
        if i == 'C':
            message = C(message)
        if i == 'J':
            message = J(message)
        if i == 'E':
            message = E(message)
        if i == 'A':
            message = A(message)
        if i == 'P':
            message = P(message)
        if i == 'M':
            message = M(message)
    ans.append(message)

[print(i) for i in ans]

Solution (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)

Solution (Python):
def main():
    s = input()
    ans = list(input())
    s = reversed(s)
    for si in s:
        if si == "C":
            ans = ans[1:] + [ans[0]]
        if si == "J":
            ans = [ans[-1]] + ans[:-1]
        if si == "E":
            tmp = [ans[len(ans) // 2]] if len(ans) & 1 else []
            ans = ans[len(ans) // 2 + (len(ans) & 1):] + tmp + ans[: len(ans) // 2]
        if si == "A":
            ans = ans[::-1]
        if si == "M":
            for i in range(len(ans)):
                if ans[i].isdecimal():
                    ans[i] = int(ans[i])
                    ans[i] = str((ans[i] + 1) % 10)
        if si == "P":
            for i in range(len(ans)):
                if ans[i].isdecimal():
                    ans[i] = int(ans[i])
                    ans[i] = str((ans[i] - 1) % 10)

    print("".join(ans))



if __name__ == "__main__":
    for _ in range(int(input())):
        main()

Solution (Python):

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


if __name__ == "__main__":
    solve()