#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string p, m;
    cin >> p >> m;
    reverse(p.begin(), p.end());
    for (size_t i = 0; i < p.length(); i++) {
      if (p[i] == 'J') {
        char tmp = m[m.length() - 1];
        for (size_t j = m.length() - 1; j > 0; j--) {
          m[j] = m[j - 1];
        }
        m[0] = tmp;
        break;
      }
      if (p[i] == 'C') {
        char tmp = m[0];
        for (size_t j = 0; j < m.length(); j++) {
          if (j == m.length() - 1)
            m[j] = tmp;
          else
            m[j] = m[j + 1];
        }
        break;
      }
      if (p[i] == 'E') {
        string tmp = m.substr(0, m.length() / 2);
        m = m.substr((m.length() + 1) / 2);
        m.append(tmp);
        break;
      }
      if (p[i] == 'A') {
        reverse(m.begin(), m.end());
        break;
      }
      if (p[i] == 'P') {
        for (size_t j = 0; j < m.length(); j++) {
          if ('1' <= m[j] && m[j] <= '9') {
            m[j]--;
          } else if ('0' == m[j]) {
            m[j] = '9';
          }
        }
        break;
      }
      if (p[i] == 'M') {
        for (size_t j = 0; j < m.length(); j++) {
          if ('0' <= m[j] && m[j] <= '8') {
            m[j]++;
          } else if ('9' == m[j]) {
            m[j] = '0';
          }
        }
        break;
      }
    }
    cout << m << endl;
  }
}
