#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++) {
      switch (p[i]) {
        case '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;
        }
        case '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;
        }
        case 'E': {
          string tmp = m.substr(0, m.length() / 2);
          m.replace(0, (m.length() - 1) / 2, m);
          m.append(tmp);
          break;
        }
        case 'A': {
          reverse(m.begin(), m.end());
          break;
        }
        case '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;
        }
        case '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;
  }
}
