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

void deTransform(const char op, string& str) {
    switch (op) {
    case 'J':
        str = str.at(str.length() - 1) + str.substr(0, str.length() - 1);
        break;
    case 'C':
        str = str.substr(1, str.length() - 1) + str.at(0);
        break;
    case 'E':
        if (str.length() % 2 == 0) {
            str = str.substr(str.length() / 2, str.length() / 2) + str.substr(0, str.length() / 2);
        } else {
            unsigned int middle = str.length() / 2;
            str = str.substr(middle + 1, middle) + str.at(middle) + str.substr(0, middle);
        }
        break;
    case 'A':
        reverse(str.begin(), str.end());
        break;
    case 'P':
        for (string::iterator it = str.begin(); it != str.end(); it++) {
            if (*it == '0') {
                *it = '9';
            } else if ('0' < *it && *it <= '9') {
                (*it)--;
            }
        }
        break;
    case 'M':
        for (string::iterator it = str.begin(); it != str.end(); it++) {
            if (*it == '9') {
                *it = '0';
            } else if ('0' <= *it && *it < '9') {
                (*it)++;
            }
        }
        break;
    }
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        string messengers, message;
        cin >> messengers >> message;
        for (string::reverse_iterator rit = messengers.rbegin(); rit != messengers.rend(); rit++) {
            deTransform(*rit, message);
        }
        cout << message << endl;
    }
}