#include <algorithm>
#include <iostream>

using namespace std;

string rotate_left (string s) {
    return s.substr(1, s.size() - 1) + s.substr(0, 1);
}

string rotate_right (string s) {
    return s.substr(s.size() - 1, 1) + s.substr(0, s.size() - 1);
}

string swap_half (string s) {
    bool even = s.size() % 2 == 0;
    int h = s.size() /2;
    if (even) {
        return s.substr(h, h) + s.substr(0, h);
    } else {
        return s.substr(h + 1, h) + s.substr(h, 1) + s.substr(0, h);
    }
}

string rev(string s) {
    reverse(begin(s), end(s));
    return s;
}

string inc_dig (string s) {
    string ret = "";
    for (char c : s) {
        if(isdigit(c)) {
            ret += ((c - '0') + 1) % 10 + '0';
        } else {
            ret += c;
        }
    }
    return ret;
}

string dec_dig (string s) {
    string ret = "";
    for (char c : s) {
        if(isdigit(c)) {
            ret += ((c - '0') + 9) % 10 + '0';
        } else {
            ret += c;
        }
    }
    return ret;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        string opers, s;
        cin >> opers >> s;
        for (auto op = opers.rbegin(); op != opers.rend(); ++op) {
            if(*op == 'J') {
                s = rotate_right(s);
            } else if (*op == 'C') {
                s = rotate_left(s);
            } else if (*op == 'E') {
                s = swap_half(s);
            } else if (*op == 'A') {
                s = rev(s);
            } else if (*op == 'P') {
                s = dec_dig(s);
            } else if (*op == 'M') {
                s = inc_dig(s);
            }
        }
        cout << s << endl;
        
    }
    return 0;
}