#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string s, t;
  char c, d;
  deque<char> de;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s >> t;
    de.clear();
    for (int j = 0; j < t.size(); j++) {
      de.push_back(t[j]);
    }
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'J') {
        c = de.front();
        de.pop_front();
        de.push_back(c);
      } else if (s[j] == 'C') {
        c = de.back();
        de.pop_back();
        de.push_front(c);
      } else if (s[j] == 'A') {
        for (int k = 0; k < de.size() / 2; k++) {
          swap(de[k], de[de.size() - k - 1]);
        }
      } else if (s[j] == 'E') {
        c = de.front();
        de.pop_front();
        de.push_back(c);
        c = de.front();
        de.pop_front();
        de.push_back(c);
      } else if (s[j] == 'P') {
        for (int k = 0; k < de.size(); k++) {
          if (de[k] >= '0' && de[k] <= '8') {
            de[k]++;
          } else if (de[k] == '9') {
            de[k] = '0';
          }
        }
      } else if (s[j] == 'M') {
        for (int k = 0; k < de.size(); k++) {
          if (de[k] >= '1' && de[k] <= '9') {
            de[k]--;
          } else if (de[k] == '0') {
            de[k] = '9';
          }
        }
      }
    }
    for (int j = 0; j < de.size(); j++) {
      cout << de[j];
      if (j == de.size() - 1) cout << endl;
    }
  }
  return 0;
}
