#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    for(int i=0; i<n; ++i) {
        string order, message;
        cin >> order >> message;
        reverse(order.begin(), order.end());

        for(int j=0; j<order.size(); ++j) {
            if(order[j] == 'J') {
                message = message.substr(message.size()-1, 1) + message.substr(0, message.size()-1);
            } else if(order[j] == 'C') {
                message = message.substr(1, message.size()-1) + message.substr(0, 1);
            } else if(order[j] == 'E') {
                string left = message.substr(0, message.size()/2);
                string right = message.substr((message.size()+1)/2, message.size()/2);
                if(message.size()%2 == 0) {
                    message = right + left;
                } else {
                    string mid = message.substr(message.size()/2, 1);
                    message = right + mid + left;
                }
            } else if(order[j] == 'A') {
                reverse(message.begin(), message.end());
            } else if(order[j] == 'P') {
                for(int k=0; k<message.size(); ++k) {
                    char c = message[k];
                    if('0' <= c && c <= '9') {
                        message[k] = '0' + (c-'0'+9)%10;
                    }
                }
            } else if(order[j] == 'M') {
                for(int k=0; k<message.size(); ++k) {
                    char c = message[k];
                    if('0' <= c && c <= '9') {
                        message[k] = '0' + (c-'0'+1)%10;
                    }
                }
            }
        }
        cout << message << endl;
    }
}
