#include <bits/stdc++.h>
using namespace std;
string o, m;
void solve() {
  int len = (int)o.size();
  int sz = (int)m.size();
  for (int i = len - 1; i >= 0; i--) {
    switch (o[i]) {
      case 'J':
        if (sz == 1) break;
        m = m[sz - 1] + m.substr(0, sz - 1);
        break;
      case 'C':
        if (sz == 1) break;
        m = m.substr(1, sz - 1) + m[0];
        break;
      case 'E':
        if (sz == 1) break;
        if (sz % 2) {
          m = m.substr(sz / 2 + 1, sz / 2) + m[sz / 1] + m.substr(0, sz / 2);
        } else {
          m = m.substr(sz / 2, sz / 2) + m.substr(0, sz / 2);
        }
        break;
      case 'A':
        reverse(m.begin(), m.end());
        break;
      case 'P':
        for (int i = 0; i < sz; i++) {
          if ('1' <= m[i] && m[i] <= '9') {
            char c = m[i] - 1;
            m[i] = c;
          } else if (m[i] == '0')
            m[i] = '9';
        }
        break;
      case 'M':
        for (int i = 0; i < sz; i++) {
          if ('0' <= m[i] && m[i] <= '8') {
            char c = m[i] + 1;
            m[i] = c;
          } else if (m[i] == '9')
            m[i] = '0';
        }
        break;
      default:
        break;
    }
  }
  cout << m << endl;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> o;
    cin >> m;
    solve();
  }
  return 0;
}
