#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string o;
    cin >> o;
    string m;
    cin >> m;
    for (int i = o.length() - 1; i >= 0; --i) {
      switch (o[i]) {
        case 'A':
          reverse((m).begin(), (m).end());
          break;
        case 'J':
          rotate(m.rbegin(), m.rbegin() + 1, m.rend());
          break;
        case 'C':
          rotate(m.begin(), m.begin() + 1, m.end());
          break;
        case 'P': {
          auto it = m.begin();
          while (it != m.end()) {
            if (isdigit(*it) && --(*it) < '0') *it = '9';
            ++it;
          }
          break;
        }
        case 'M': {
          auto it = m.begin();
          while (it != m.end()) {
            if (isdigit(*it) && ++(*it) > '9') *it = '0';
            ++it;
          }
          break;
        }
        case 'E': {
          int len = m.length();
          string f = m.substr(0, len / 2);
          char mdl = m.length() % 2 ? m[len / 2] : 0;
          string b = m.substr((len + 1) / 2);
          m = b + mdl + f;
          break;
        }
        default:
          break;
      }
    }
    cout << m << endl;
  }
  return EXIT_SUCCESS;
}
