#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

string func(string message, char type) {
  int m = message.size();
  string res = message;
  if (type == 'J') {
    res = string(1, message[m-1]) + message.substr(0, m-1);
  } else if (type == 'C') {
    res = message.substr(1) + string(1, message[0]);
  } else if (type == 'E') {
    res = message.substr((m+1)/2) + (m%2?string(1, message[m/2]):"") + message.substr(0, m/2);
  } else if (type == 'A') {
    reverse(ALL(res));
  } else if (type == 'P') {
    REP(i, m)
      if (isdigit(message[i]))
        res[i] = (message[i] == '0') ? '9' : message[i]-1;
  } else if (type == 'M') {
    REP(i, m)
      if (isdigit(message[i]))
        res[i] = (message[i] == '9') ? '0' : message[i]+1;
  }
  return res;  
}
int main() {
  int n;
  cin >> n;
  while(n--) {
    string order, message;
    cin >> order >> message;
    for (int i=order.size()-1; i>=0; --i) {
      message = func(message, order[i]);
    }
    cout << message << endl;
  }
}