#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

string J(string S) {
    char c = S.front();
    S.erase(begin(S));
    return S + c;
}
string C(string S) {
    char c = S.back();
    S.pop_back();
    return c + S;
}
string E(string S) {
    string a, b;
    a = S.substr(0, S.length() / 2);
    b = S.substr(S.length() / 2);
    return b + a;
}
string A(string S) {
    REVERSE(S);
    return S;
}
string M(string S) {
    REP(i, S.length()) {
        if (isdigit(S[i])) {
            int n = S[i] - '0';
            n++;
            if (n == 10) n = 0;
            S[i] = n + '0';
        }
    }
    return S;
}
string P(string S) {
    REP(i, S.length()) {
        if (isdigit(S[i])) {
            int n = S[i] - '0';
            n--;
            if (n == -1) n = 9;
            S[i] = n + '0';
        }
    }
    return S;
}

void solve(int N) {
    vector<pair<string, string>> m(N);
    for_each(ALL(m), [](auto &e){cin >> e.first >> e.second;});
    FORE(el, m) {
        string ans = el.second;
        FORE(e, el.first) {
            switch (e) {
            case 'J':
                ans = J(ans);
                break;
            case 'C':
                ans = C(ans);
                break;
            case 'E':
                ans = E(ans);
                break;
            case 'A':
                ans = A(ans);
                break;
            case 'P':
                ans = P(ans);
                break;
            case 'M':
                ans = M(ans);
                break;
            }
        }
        cout << ans << endl;
    }
    return;
}

signed main(void) {
    int n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    while (cin >> n, n) {
        solve(n);
        return 0;
    }
}