#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
//#define int long long
using namespace std;

string hoge(string m, char c){
    int h = m.size() / 2;
    int sz = m.size();
    string res = "";
    if(c == 'J'){
        res = m[sz - 1] + m.substr(0, sz - 1);
    }else if(c == 'C'){
        res = m.substr(1, sz - 1) + m[0];
    }else if(c == 'E'){
        rep(i, 0, h){
            swap(m[i], m[(sz + 1) / 2 + i]);
        }
        res = m;
    }else if(c == 'A'){
        res = m;
        reverse(all(res));
    }else if(c == 'P'){
        rep(i, 0, sz){
            int tmp = m[i] - '0';
            if(0 < tmp && tmp <= 9) m[i] = '0' + tmp - 1;
            else if(tmp == 0) m[i] = '9';
        }
        res = m;
    }else if(c == 'M'){
        rep(i, 0, sz){
            int tmp = m[i] - '0';
            if(0 <= tmp && tmp < 9) m[i] = '0' + tmp + 1;
            else if(tmp == 9) m[i] = '0';
        }
        res = m;
    }
    return res;
}

signed main(){
    int n;
    cin >> n;
    rep(x, 0, n){
        string s, m;;
        cin >> s >> m;
        repb(i, s.size() - 1, 0){
            m = hoge(m, s[i]);
        }
        cout << m << endl;
    }
}