#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int n;
    cin >> n;
    while (n--) {
        string s;
        cin >> s;
        string t;
        cin >> t;
        int m = t.size();

        reverse(all(s));
        rep(i, s.size()) {
            if (s[i] == 'J') {
                t = t[m - 1] + t.substr(0, m - 1);
            }
            if (s[i] == 'C') {
                t = t.substr(1) + t[0];
            }
            if (s[i] == 'E') {
                string u = "";
                u += t.substr((m + 1) / 2);
                if (m % 2 == 1) u += t[m / 2];
                u += t.substr(0, m / 2);
                t = u;
            }
            if (s[i] == 'A') {
                reverse(all(t));
            }
            if (s[i] == 'P') {
                rep(j, m) {
                    if (isdigit(t[j])) {
                        t[j] = (t[j] - '0' - 1 + 10) % 10 + '0';
                    }
                }
            }
            if (s[i] == 'M') {
                rep(j, m) {
                    if (isdigit(t[j])) {
                        t[j] = (t[j] - '0' + 1) % 10 + '0';
                    }
                }
            }
        }

        cout << t << endl;
    }
}