#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  while (N--) {
    string a, s;
    cin >> a >> s;
    reverse(a.begin(), a.end());
    for (int i = 0; i < a.size(); i++) {
      switch (a[i]) {
        case 'A':
          reverse(s.begin(), s.end());
          break;
        case 'J':
        case 'C':
          s = s.substr(1) + s[0];
          break;
          s = s.substr(1) + s.substr(0, 1);
          break;
        case 'M':
          for (int i = 0; i < s.size(); i++) {
            if ('0' <= s[i] && s[i] <= '9') {
              if (s[i] == '9')
                s[i] = '0';
              else
                s[i]++;
            }
          }
          break;
        case 'P':
          for (int i = 0; i < s.size(); i++) {
            if ('0' <= s[i] && s[i] <= '9') {
              if (s[i] == '0')
                s[i] = '9';
              else
                s[i]--;
            }
          }
          break;
        case 'E':
          string res = "";
          for (int i = s.size() / 2; i < s.size(); i++) res += s[i];
          for (int i = 0; i < s.size() / 2; i++) res += s[i];
          s = res;
          break;
      }
    }
    cout << s << endl;
  }
}
