#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void)
{
  int n;
  cin >> n;
  REP(i, n) {
    string s, t;
    cin >> t >> s;
    for(int j=t.size()-1; j>=0; --j) {
      if(t[j] == 'C') {
        string tmp;
        FOR(i, 1, s.size()) tmp += s[i];
        tmp += s[0];
        s = tmp;
      } else if(t[j] == 'J') {
        string tmp;
        tmp += s[s.size()-1];
        REP(i, s.size()-1) tmp += s[i];
        s = tmp;
      } else if(t[j] == 'E') {
        string tmp;
        FOR(i, s.size()/2 + !!(s.size()%2), s.size()) tmp += s[i];
        if(s.size()%2) tmp += s[s.size()/2];
        REP(i, s.size()/2) tmp += s[i];
        s = tmp;
      } else if(t[j] == 'A') {
        REP(i, s.size()/2) swap(s[i], s[s.size()-1-i]);
      } else if(t[j] == 'M') {
        REP(i, s.size()) {
          if('0' <= s[i] && s[i] <= '8') s[i]++;
          else if(s[i] == '9') s[i] = '0';
        }
      } else if(t[j] == 'P') {
        REP(i, s.size()) {
          if('1' <= s[i] && s[i] <= '9') s[i]--;
          else if(s[i] == '0') s[i] = '9';
        }
      }
      //cout << j << " " << t[j] << " " << s << endl;
    }
    cout << s << endl;
  }

  return 0;
}