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

string J(string s) {
    string res = s.substr(0, s.size() - 1);
    return s[s.size() - 1] + res;
}

string C(string s) {
    string res = s.substr(1, s.size() - 1);
    return res + s[0];
}

string E(string s) {
    int n = s.size();
    string s1 = s.substr(0, n / 2);
    string s2 = s.substr((n + 1) / 2, n / 2);
    if (n % 2) s2 += s[n / 2];
    return s2 + s1;
}

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

string P(string s) {
    for (int i = 0; i < s.size(); i++) {
        if (s[i] >= '0' && s[i] <= '9') {
            if (s[i] == '0') s[i] = '9';
            else s[i]--;
        }
    }
    return s;
}

string M(string s) {
    for (int i = 0; i < s.size(); i++) {
        if (s[i] >= '0' && s[i] <= '9') {
            if (s[i] == '9') s[i] = '0';
            else s[i]++;
        }
    }
    return s;
}

void solve() {
    string s1, s2;
    cin >> s1 >> s2;
    for (int i = s1.size() - 1; i >= 0; i--) {
        if (s1[i] == 'J') s2 = J(s2);
        else if (s1[i] == 'C') s2 = C(s2);
        else if (s1[i] == 'E') s2 = E(s2);
        else if (s1[i] == 'A') s2 = A(s2);
        else if (s1[i] == 'P') s2 = P(s2);
        else s2 = M(s2);
    }
    cout << s2 << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        solve();
    }
    return 0;
}
