#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  ll N;
  cin >> N;
  vector<string> order(N), message(N);
  REP(i, 0, N) cin >> order[i] >> message[i];

  REP(i, 0, N) {
    string s(message[i]);
    ll l = message[i].length();

    REP(j, 0, order[i].size()) {
      char c = order[i][order[i].size() - j - 1];
      string t(s);

      if(c == 'J') {
        REP(k, 0, l) t[(k + 1 + l) % l] = s[k];
      }
      if(c == 'C') {
        REP(k, 0, l) t[(k - 1 + l) % l] = s[k];
      }
      if(c == 'E') {
        REP(k, 0, l / 2) swap(t[k], t[k + l / 2 + l % 2]);
      }
      if(c == 'A') {
        reverse(t.begin(), t.end());
      }
      if(c == 'P') {
        REP(k, 0, l) if('0' <= t[k] && t[k] <= '9') {
          t[k] = (t[k] - 1 - '0' + 10) % 10 + '0';
        }
      }
      if(c == 'M') {
        REP(k, 0, l) if('0' <= t[k] && t[k] <= '9') {
          t[k] = (t[k] + 1 - '0' + 10) % 10 + '0';
        }
      }

      swap(s, t);
    }

    cout << s << endl;
  }
}

