#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 100010;

int main(){
    int q;
    scanf("%d", &q);
    auto f = [](string s, char c){
        int K = s.size();
        string t;
        if(c == 'J') t = s[K-1] + s.substr(0, K-1);
        if(c == 'C') t = s.substr(1) + s[0];
        if(c == 'E'){
            if(K%2 == 0) t = s.substr(K/2) + s.substr(0, K/2);
            else t = s.substr((K+1)/2) + s[K/2] + s.substr(0, K/2);
        }
        if(c == 'A'){
            reverse(s.begin(), s.end());
            t = s;
        }
        if(c == 'P'){
            t = s;
            for(auto &i: t)if(isdigit(i)) i = (i-48-1+10)%10 + 48;
        }
        if(c == 'M'){
            t = s;
            for(auto &i: t)if(isdigit(i)) i = (i-48+1)%10 + 48;
        }
        return t;
    };
    while(q--){
        string t, s;
        cin >> t >> s;
        reverse(t.begin(), t.end());
        for(auto c: t) s = f(s, c);
        cout << s << "\n";
    }
}
