#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const double PI = acos(-1.0);
int n;
inline string j(string a) {
  string res;
  res = a.substr(1, a.length() - 1);
  res += a[0];
  return res;
}
inline string c(string a) {
  string res;
  res = a[a.length() - 1];
  res += a.substr(0, a.length() - 1);
  return res;
}
inline string e(string a) {
  string res, b, c;
  if ((int)a.length() & 1) {
    b = a.substr(0, a.length() / 2);
    c = a.substr(a.length() / 2 + 1, a.length() / 2);
    res = c;
    res += a[a.length() / 2];
    res += b;
    return res;
  } else {
    b = a.substr(0, a.length() / 2);
    c = a.substr(a.length() / 2, a.length() / 2);
    res = c + b;
    return res;
  }
}
inline string a(string a) {
  reverse(a.begin(), a.end());
  return a;
}
inline string p(string a) {
  char b[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
  for (int i = 0; i < a.length(); i++) {
    for (int j = 0; j < 10; j++) {
      if (a[i] == b[j]) {
        if (j < 9) {
          a[i] = b[j + 1];
        } else {
          a[i] = b[0];
        }
        j = 10;
      }
    }
  }
  return a;
}
inline string m(string a) {
  char b[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
  for (int i = 0; i < a.length(); i++) {
    for (int j = 0; j < 10; j++) {
      if (a[i] == b[j]) {
        if (j > 0) {
          a[i] = b[j - 1];
        } else {
          a[i] = b[9];
        }
        j = 10;
      }
    }
  }
  return a;
}
int main() {
  cin >> n;
  for (int r = 0; r < n; r++) {
    for (int i = 0; i < n; i++) {
      string s, t;
      cin >> s >> t;
      for (int k = s.length() - 1; k >= 0; k--) {
        if (s[k] == 'A') {
          t = a(t);
        } else if (s[k] == 'J') {
          t = c(t);
        } else if (s[k] == 'M') {
          t = p(t);
        } else if (s[k] == 'P') {
          t = m(t);
        } else if (s[k] == 'E') {
          t = e(t);
        } else {
          t = j(t);
        }
      }
      cout << t << endl;
    }
  }
  return 0;
}
