#include <iostream>
#include <string>
#include <algorithm>
#include <deque>
#include <cctype>

using namespace std;

int main(){
    int n;
    cin >> n;
    for (int _ = 0; _ < n; _++){
        string op, s;
        cin >> op >> s;
        reverse(op.begin(), op.end());
        deque<char> str(s.begin(), s.end());
        for (char c : op){
            if (c == 'C'){
                char t = str.front();
                str.pop_front();
                str.push_back(t);
            }
            else if (c == 'J'){
                char t = str.back();
                str.pop_back();
                str.push_front(t);
            }
            else if (c == 'E'){
                for (int i = 0; i < str.size() / 2; i++){
                    swap(str[i], str[i + (str.size() + 1) / 2]);
                }
            }
            else if (c == 'A'){
                reverse(str.begin(), str.end());
            }
            else if (c == 'M'){
                for (char & d : str){
                    if (isdigit(d)){
                        d++;
                        if (d == '9' + 1){
                            d = '0';
                        }
                    }
                }
            }
            else if (c == 'P'){
                for (char & d : str){
                    if (isdigit(d)){
                        d--;
                        if (d == '0' - 1){
                            d = '9';
                        }
                    }
                }
            }
        }
        for (char c : str){
            cout << c;
        }
        cout << endl;
    }
}