#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, l, m;
  string s, t;
  char u, p;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    cin >> t;
    l = s.size();
    m = t.size();
    for (int i = l-1; i >= 0; --i) {
      p = s[i];
      if (p == 'P') {
	for (int j = 0; j < m; ++j) {
	  if (t[j] == '0') t[j] = '9';
	  else if ('0' < t[j] && t[j] <= '9') {
	    t[j] = t[j] - 1;
	  }
	}
      } else if (p == 'M') {
	for (int j = 0; j < m; ++j) {
	  if (t[j] == '9') t[j] = '0';
	  else if ('0' <= t[j] && t[j] < '9') {
	    t[j] = t[j] + 1;
	  }
	}
      } else if (p == 'C') {
	u = t[0];
	for (int j = 0; j < m-1; ++j) t[j] = t[j+1];
	t[m-1] = u;
      } else if (p == 'A') {
	for (int j = 0; j < m/2; ++j) {
	  u = t[m-1-j];
	  t[m-1-j] = t[j];
	  t[j] = u;
	}
      } else if (p == 'E') {
	for (int j = 0; j < m/2; ++j) {
	  u = t[m/2+m%2+j];
	  t[m/2+m%2+j] = t[j];
	  t[j] = u;
	}
      } else if (p == 'J') {
	u = t[m-1];
	for (int j = m-1; j > 0; --j) t[j] = t[j-1];
	t[0] = u;
      }
      //      cout << p << " " << t << endl;
    }
    cout << t << endl;
  }
  return 0;
}