#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string a, b;
  char c[100], d[100];
  cin >> n;
  for (int u = 0; u < n; u++) {
    cin >> a >> b;
    for (int i = 0; i < b.size(); i++) d[i] = b[i];
    for (int i = a.size() - 1; i >= 0; i--) {
      if (a[i] == 'J') {
        for (int i = 0; i < b.size() - 1; i++) c[i + 1] = d[i];
        c[0] = d[b.size() - 1];
      }
      if (a[i] == 'C') {
        for (int i = 0; i < b.size() - 1; i++) c[i] = d[i + 1];
        c[b.size() - 1] = d[0];
      }
      if (a[i] == 'E') {
        for (int i = 0; i < b.size(); i++) {
          if (i < b.size() / 2)
            c[i + b.size() - b.size() / 2] = d[i];
          else
            c[i - b.size() + b.size() / 2] = d[i];
        }
      }
      if (a[i] == 'A') {
        for (int i = 0; i < b.size(); i++) c[i] = d[b.size() - 1 - i];
      }
      if (a[i] == 'P') {
        for (int i = 0; i < b.size(); i++) {
          bool check = false;
          if (d[i] >= '1' && d[i] <= '9') {
            c[i] = d[i] - 1;
            check = true;
          }
          if (d[i] == '0') {
            c[i] = '9';
            check = true;
          }
          if (check == false) c[i] = d[i];
        }
      }
      if (a[i] == 'M') {
        for (int i = 0; i < b.size(); i++) {
          bool check = false;
          if (d[i] >= '0' && b[i] <= '8') {
            c[i] = d[i] + 1;
            check = true;
          }
          if (d[i] == '9') {
            c[i] = '0';
            check = true;
          }
          if (check == false) c[i] = d[i];
        }
      }
      for (int i = 0; i < b.size(); i++) d[i] = c[i];
    }
    for (int i = 0; i < b.size(); i++) cout << d[i];
    cout << endl;
  }
  return 0;
}
