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

char dec(char c) {
    if(isdigit(c)) {
        int d = c - '0';
        d = (d - 1 + 10) % 10;
        return '0' + d;
    }
    return c;
}

char inc(char c) {
    if(isdigit(c)) {
        int d = c - '0';
        d = (d + 1) % 10;
        return '0' + d;
    }
    return c;
}

int main() {
    int N; cin >> N;
    while(N--) {
        string op; cin >> op;
        reverse(op.begin(), op.end());
        string s; cin >> s;
        for(char c : op) {
            if(c == 'J') s = s.substr(s.length() - 1) + s.substr(0, s.length() - 1);
            if(c == 'C') s = s.substr(1) + s.substr(0, 1);
            if(c == 'E') {
                if(s.length() & 1) {
                    s = s.substr(s.length() / 2 + 1) + s.substr(s.length() / 2, 1) + s.substr(0, s.length() / 2);
                }
                else {
                    s = s.substr(s.length() / 2) + s.substr(0, s.length() / 2);
                }
            }
            if(c == 'A') reverse(s.begin(), s.end());
            if(c == 'P') for(int i = 0; i < (int)s.length(); i++) s[i] = dec(s[i]);
            if(c == 'M') for(int i = 0; i < (int)s.length(); i++) s[i] = inc(s[i]);
        }
        cout << s << endl;
    }
}