#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    for (int j = 0; j < a.size(); j++) {
      if (a[j] == 'J') {
        rotate(b.rbegin(), b.rbegin() + 1, b.rend());
      }
      if (a[j] == 'C') {
        rotate(b.begin(), b.begin() + 1, b.end());
      }
      if (a[j] == 'E') {
        stringstream ss;
        for (int k = b.size() / 2; k < b.size(); k++) {
          ss << b[k];
        }
        for (int k = 0; k < b.size() / 2; k++) {
          ss << b[k];
        }
        b = ss.str();
      }
      if (a[j] == 'A') {
        reverse(b.begin(), b.end());
      }
      if (a[j] == 'P') {
        for (int k = 0; k < b.size(); k++) {
          if (b[k] == '0') b[k] = '9';
          if ('1' <= b[k] && b[k] <= '9') b[k]--;
        }
      }
      if (a[j] == 'M') {
        for (int k = 0; k < b.size(); k++) {
          if (b[k] == '9') b[k] = '0';
          if ('0' <= b[k] && b[k] <= '8') b[k]++;
        }
      }
    }
    cout << b << endl;
  }
  return 0;
}
