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

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)
#define all(c) (c).begin(), (c).end()

void J(string& s){
    rotate(s.begin(), s.end() - 1, s.end());
}

void C(string& s){
    rotate(s.begin(), s.begin() + 1, s.end());
}

void E(string& s){
    int n = (int)s.size();
    swap_ranges(s.begin(), s.begin() + (n >> 1), s.begin() + (n >> 1) + (n & 1));
}

void A(string& s){
    reverse(all(s));
}

void P(string& s){
    rep(i, (int)s.size())if(isdigit(s[i]))s[i] = (s[i] + 9 - '0') % 10 + '0';
}

void M(string& s){
    rep(i, (int)s.size())if(isdigit(s[i]))s[i] = (s[i] + 1 - '0') % 10 + '0';
}

int main(){
    int n;
    cin >> n; cin.ignore();
    while(n--){
        string order, mes;
        cin >> order >> mes;
        reverse(all(order));
        rep(i, (int)order.size()){
            if(order[i] == 'J')J(mes);
            if(order[i] == 'C')C(mes);
            if(order[i] == 'E')E(mes);
            if(order[i] == 'A')A(mes);
            if(order[i] == 'P')P(mes);
            if(order[i] == 'M')M(mes);
        }
        cout << mes << '\n';
    }
    return 0;
}