#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

string s, t;

void j() {
    char c = t[0];
    for (int i=1; i<t.size(); ++i) {
        t[i-1] = t[i];
    }
    t[t.size()-1] = c;
}

void c() {
    char c = t[t.size()-1];
    for (int i=t.size()-2; i>=0; --i) {
        t[i+1] = t[i];
    }
    t[0] = c;
}

void a() {
    reverse(t.begin(), t.end());
}

void e() {
    if (t.size()%2) {
        char c = t[t.size()/2];
        string a = t.substr(0, t.size()/2), b = t.substr(t.size()/2+1, t.size()/2);
        t = b + c + a;
    }
    else {
        string a = t.substr(0, t.size()/2), b = t.substr(t.size()/2, t.size()/2);
        t = b + a;
    }
}

void p() {
    rep(i, t.size()) {
        if (t[i] == '9') t[i] = '0';
        else if ('0' <= t[i] and t[i] <= '9') t[i]++;
    }
}

void m() {
    rep(i, t.size()) {
        if (t[i] == '0') t[i] = '9';
        else if ('0' <= t[i] and t[i] <= '9') t[i]--;
    }
}

int main() {
    int n;
    cin >> n;

    rep(i, n) {

        cin >> s >> t;

        reverse(s.begin(), s.end());
        rep(i, s.size()) {
            switch(s[i]) {
                case 'A': a(); break;
                case 'J': c(); break;
                case 'C': j(); break;
                case 'P': m(); break;
                case 'M': p(); break;
                case 'E': e(); break;
            }
        }

        cout << t << endl;

    }
}