#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':
          s = s.substr(s.size() - 1) + s.substr(0, s.size() - 1);
          break;
        case 'C':
          s = s.substr(1) + s.substr(s.size() - 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':
          int l = s.size() / 2;
          if (s.size() % 2)
            s = s.substr(s.size() - l) + s[l] + s.substr(0, l);
          else
            s = s.substr(s.size() - l) + s.substr(0, l);
          break;
      }
    }
    cout << s << endl;
  }
}
