#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string s, t;
    cin >> s >> t;
    list<char> p;
    for (int i = t.length() - 1; i >= 0; i--) {
      p.push_front(t[i]);
    }
    for (int i = s.length() - 1; i >= 0; i--) {
      for (list<char>::iterator it = p.begin(); it != p.end(); ++it)
        cout << *it;
      cout << endl;
      if (s[i] == 'J') {
        p.push_front(p.back());
        p.pop_back();
      } else if (s[i] == 'C') {
        p.push_back(p.front());
        p.pop_front();
      } else if (s[i] == 'E') {
        if (t.length() % 2 == 0) {
          for (int j = 0; j < t.length() / 2; j++) {
            p.push_front(p.back());
            p.pop_back();
          }
        } else {
          for (int j = 0; j < (t.length() - 1) / 2; j++) {
            p.push_front(p.back());
            p.pop_back();
          }
          list<char>::iterator it;
          it = p.begin();
          for (int j = 0; j < (t.length() - 1) / 2; j++) it++;
          p.insert(it, p.back());
          p.pop_back();
        }
      } else if (s[i] == 'A') {
        p.reverse();
      } else if (s[i] == 'P') {
        for (list<char>::iterator it = p.begin(); it != p.end(); ++it) {
          if (isdigit(*it)) {
            int a = *it - '0';
            a += 9;
            a %= 10;
            *it = a + '0';
          }
        }
      } else {
        for (list<char>::iterator it = p.begin(); it != p.end(); ++it) {
          if (isdigit(*it)) {
            int a = *it - '0';
            a += 11;
            a %= 10;
            *it = a + '0';
          }
        }
      }
    }
    for (list<char>::iterator it = p.begin(); it != p.end(); ++it) cout << *it;
    cout << endl;
  }
  return 0;
}
