using System;
using System.Linq;
using System.Collections.Generic;




namespace ConsoleApplication1 {

    

    class ClassMain {


        public static string revJ(string str) {
            string result = ""+str[str.Length-1];
            for (int i = 0; i < str.Length-1; i++) {
                result += str[i];
            }
            return result;
        }
        public static string revC(string str) {
            string result = "";
            for (int i = 1; i < str.Length ; i++) {
                result += str[i];
            }
            result += str[0];
            return result;
        }
        public static string revE(string str) {
            string result = "";
            string former = str.Substring(0, str.Length / 2);
            string latter = str.Substring((str.Length - 1) / 2 + 1);
            if (str.Length % 2 == 0) { result = latter + former; }
            else { result = latter + str[str.Length / 2] + former; }
            return result;
        }
        public static string revA(string str) {
            char[] chrs = str.ToCharArray();
            Array.Reverse(chrs);
            return new string(chrs);
        }
        public static string revP(string str) {
            string result = "";
            for (int i = 0; i < str.Length; i++) {
                if(char.IsDigit(str[i])){
                    if(str[i]=='0'){result += '9';}
                    else{
                    int decre = str[i]-'0'-1;
                        result += decre+"";
                    } 
                }else{
                    result+=str[i];
                }
            }
            return result;
        }
        public static string revM(string str) {
            string result = "";
            for (int i = 0; i < str.Length; i++) {
                if(char.IsDigit(str[i])){
                    if(str[i]=='9'){result += '0';}
                    else{int incre = str[i]-'0'+1;

                        result += incre+""; }
                }else{result+=str[i];}
            }
            return result;
        }





        public static void Main(String[] args) {
           string line;
           while ((line = Console.ReadLine()) != null) {
                //int[] ints = line.Split(' ').Select(int.Parse).ToArray();
                //int n = ints[0];
                int n = int.Parse(line);                
                if (n == 0) { break; }
                
               for (int i = 0; i < n; i++) {
                   line = Console.ReadLine();
                   string str = Console.ReadLine();
                   for (int j = line.Length - 1; j >= 0; j--) {
                       char order = line[j];
                       if (order == 'A') {
                           str = revA(str);
                       }
                       if (order == 'J') {
                           str = revJ(str);
                       }
                       if (order == 'M') {
                           str = revM(str);
                       }
                       if (order == 'P') {
                           str = revP(str);
                       }
                       if (order == 'E') {
                           str = revE(str);
                       }
                       if (order == 'C') {
                           str = revC(str);
                       }
                   }
                   Console.WriteLine(str);
               
               }
               break;
                //Console.WriteLine();
                
            }
        }

    }

    


}

Measured memory_bytes: 5668