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 <vector> 
#include <cmath> 
#include <algorithm> 
#include <cstdlib> 
#include <ctime> 
#include <cstdio> 
#include <functional> 
#include <set> 
#include <sstream> 
#include <cctype>
#include <stack>
#include <queue>
#include <cstring>
#include <map>
 
using namespace std;

string J(string s){

	int n=s.size();
	string t;
	for(int i=0;i<n;i++){
		t+=s[(n-1+i)%n];
	}
	return t;

}

string C(string s){
	int n=s.size();
	string t;
	for(int i=0;i<n;i++){
		t+=s[(1+i)%n];
	}
	return t;
}

string E(string s){
	string t;
	if(s.size()%2){
		string left=s.substr(0,s.size()/2);
		char mid=s[s.size()/2];
		string right=s.substr(s.size()/2+1);
		t=right+mid+left;
	}else{
		string left=s.substr(0,s.size()/2);
		string right=s.substr(s.size()/2);
		t=right+left;
	}

	return t;
}

string A(string s){
	string t=s;
	reverse(t.begin(),t.end());
	return t;
}

string P(string s){
	string t=s;
	for(int i=0;i<t.size();i++){
		if( '0'<=t[i] && t[i]<='9' ){
			if(t[i]=='0') t[i]='9';
			else t[i]--;
		}
	}
	return t;
}

string M(string s){
	string t=s;
	for(int i=0;i<t.size();i++){
		if( '0'<=t[i] && t[i]<='9' ){
			if(t[i]=='9') t[i]='0';
			else t[i]++;
		}
	}
	return t;
}

int main(){

	int n;
	cin>>n;

	for(int i=0;i<n;i++){
		string t,s;
		cin>>t>>s;
		for(int j=t.size()-1;j>=0;j--){
			if(t[j]=='J') s=J(s);
			else if(t[j]=='C') s=C(s);
			else if(t[j]=='E') s=E(s);
			else if(t[j]=='A') s=A(s);
			else if(t[j]=='P') s=P(s);
			else if(t[j]=='M') s=M(s);
		}
		cout<<s<<endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };


int main() {
	int n;
	string str;
	string root;
	scanf( "%d", &n );
	while ( n-- ) {
		cin >> root;
		cin >> str;
		string copy_root = root;
		sort( copy_root.begin(), copy_root.end() );
		int move = ( upper_bound( copy_root.begin(), copy_root.end(), 'M' ) - lower_bound( copy_root.begin(), copy_root.end(), 'M' ) ) - ( upper_bound( copy_root.begin(), copy_root.end(), 'P' ) - lower_bound( copy_root.begin(), copy_root.end(), 'P' ) ) - 6;
		move %= 10;
		reverse( root.begin(), root.end() );
		for ( int i = 0; i < root.size(); i++ ) {
			if ( root[ i ] == 'A' ) {
				reverse( str.begin(), str.end() );
			}
			if ( root[ i ] == 'J' ) {
				str = str[ str.size() - 1 ] + str.substr( 0, str.size() - 1 );
			}
			if ( root[ i ] == 'C' ) {
				str = str.substr( 1, str.size() - 1 ) + str[ 0 ];
			}
			if ( root[ i ] == 'E' ) {
				if ( str.size() % 2 == 0 ) {
					str = str.substr( str.size() / 2, str.size() / 2 ) + str.substr( 0, str.size() / 2 );
				}
				else {
					str = str.substr( str.size() / 2 + 1, str.size() / 2 ) + str[ str.size() / 2 ] + str.substr( 0, str.size() / 2 );
				}
			}
		}
		for ( int i = 0; i < str.size(); i++ ) {
			if ( str[ i ] >= '0' && str[ i ] <= '9' ) {
				str[ i ] -= '0';
				str[ i ] += 10000;
				str[ i ] += move;
				str[ i ] %= 10;
				str[ i ] += '0';
			}
		}
		printf( "%s\n", str.c_str() );
	}
	return 0;
}