#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

string num = "0123456789";

string mrj(string s) {
  return s[s.length()-1] + s.substr(0, s.length()-1);
}

string missc(string s) {
  return s.substr(1, s.length()-1) + s[0];
}

string mre(string s) {
  if (s.length() % 2 == 1) return s.substr(s.length()/2+1, s.length()/2) + s[s.length()/2] + s.substr(0, s.length()/2);
  else return s.substr(s.length()/2, s.length()/2) + s.substr(0, s.length()/2);
}

string mra(string s) {
  string res = "";
  for (int i = s.length()-1; i >= 0; i--) {
    res += s[i];
  }
  return res;
}

string drp(string s) {
  for (int i = 0; i < s.length(); i++) {
    int index = (int)num.find(s[i]);
    if (index >= 0) {
      s[i] = num[(index+9)%10];
    }
  }
  return s;
}

string mrm(string s) {
  for (int i = 0; i < s.length(); i++) {
    int index = (int)num.find(s[i]);
    if (index >= 0) {
      s[i] = num[(index+1)%10];
    }
  }
  return s;
}

int main() {
  int n;
  string order, message;
  cin >> n;
  while (n--) {
    cin >> order >> message;
    for (int i = order.length()-1; i >= 0; i--) {
      if (order[i] == 'J') message = mrj(message);
      if (order[i] == 'C') message = missc(message);
      if (order[i] == 'E') message = mre(message);
      if (order[i] == 'A') message = mra(message);
      if (order[i] == 'P') message = drp(message);
      if (order[i] == 'M') message = mrm(message);
    }
    cout << message << el;
  }
}