#include <bits/stdc++.h>
using namespace std;
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  long long n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    string t;
    cin >> t;
    long long m = t.size();
    for (long long i = 0; i < (s.size()); i++) {
      if (s[i] == 'J') {
        t = t.substr(1) + t[0];
      }
      if (s[i] == 'C') {
        t = t[m - 1] + t.substr(0, m - 1);
      }
      if (s[i] == 'E') {
        string u = "";
        u += t.substr((m + 1) / 2);
        if (m % 2 == 1) u += t[m / 2];
        u += t.substr(0, m / 2);
        t = u;
      }
      if (s[i] == 'A') {
        reverse((t).begin(), (t).end());
      }
      if (s[i] == 'P') {
        for (long long j = 0; j < (m); j++) {
          if (isdigit(t[j])) {
            t[j] = (t[j] - '0' + 1) % 10 + '0';
          }
        }
      }
      if (s[i] == 'M') {
        for (long long j = 0; j < (m); j++) {
          if (isdigit(t[j])) {
            t[j] = (t[j] - '0' - 1 + 10) % 10 + '0';
          }
        }
      }
    }
    cout << t << endl;
  }
}
