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

int main() {
    int C; cin >> C;
    for (int CASE = 0; CASE < C; ++CASE) {
        string S, T; cin >> S >> T;
        int n = (int)T.size();
        reverse(S.begin(), S.end());
        for (auto c : S) {
            string nT = T;
            if (c == 'J') nT = "", nT += T.back(), nT += T.substr(0, n-1);
            else if (c == 'C') nT = T.substr(1) + T[0];
            else if (c == 'E') {
                for (int i = 0; i < n; ++i) {
                    if (i < n/2) nT[i] = T[i + (n+1)/2];
                    else if (i >= (n+1)/2) nT[i] = T[i - (n+1)/2];
                    else nT[i] = T[i];
                }
            }
            else if (c == 'A') {
                reverse(nT.begin(), nT.end());
            }
            else if (c == 'P') {
                for (int i = 0; i < n; ++i) {
                    if (T[i] >= '0' && T[i] <= '9') {
                        int val = T[i] - '0';
                        val = (val + 9) % 10;
                        nT[i] = '0' + val;
                    }
                    else nT[i] = T[i];
                }
            }
            else if (c == 'M') {
                for (int i = 0; i < n; ++i) {
                    if (T[i] >= '0' && T[i] <= '9') {
                        int val = T[i] - '0';
                        val = (val + 1) % 10;
                        nT[i] = '0' + val;
                    }
                    else nT[i] = T[i];
                }
            }
            T = nT;
        }
        cout << T << endl;
    }
}
