#include <bits/stdc++.h>
using namespace std;
 
int n;
string s,name;
 
string solve();
 
int main(){
    cin >> n;
    for(int i = 0;i < n; ++i){
        cin >> name >> s;
        cout << solve() << endl;
    }
    return 0;
}
 
string solve(){
    reverse(name.begin(),name.end());
    int nsize = name.size(), ssize = s.size(), half = s.size() / 2;
    for(int i = 0;i < nsize;++i){
        string tmp;
        switch(name[i]){
            case 'J':
                tmp = s[ssize-1] + s;
                tmp.pop_back();
                s = tmp;
                break;
            case 'C':
                for(int j = 1;j < ssize;++j)tmp += s[j];
                tmp += s[0];
                s = tmp;
                break;
            case 'E':
                for(int j = 0;j < half;++j)tmp += s[half + ssize % 2 + j];
                if(ssize%2)tmp += s[half];
                for(int j = 0;j < half;++j)tmp += s[j];
                s = tmp;
                break;
            case 'A':
                reverse(s.begin(),s.end());
                break;
            case 'P':
                for(int j = 0;j < ssize;++j)
                    if(s[j] >= '0' && s[j] <= '9')
                        s[j] = '0' + (s[j] - '0' + 9) % 10;
                break;
            case 'M':
                for(int j = 0;j < ssize;++j)
                    if(s[j] >= '0' && s[j] <= '9')
                        s[j] = '0' + (s[j] - '0' + 1) % 10;
                break;
            default:
                assert(0);
                break;
        }
    }
    return s;
}

