#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  auto decryptJ = [](const string &s) {
    return s.back() + s.substr(0, s.size() - 1);
  };
  auto decryptC = [](const string &s) { return s.substr(1) + s.front(); };
  auto decryptE = [](const string &s) {
    if (s.size() % 2 == 0) {
      return s.substr(s.size() / 2) + s.substr(0, s.size() / 2);
    } else {
      return s.substr(s.size() / 2 + 1) + s[s.size() / 2] +
             s.substr(0, s.size() / 2);
    }
  };
  auto decryptA = [](const string &s) {
    auto r = s;
    reverse(begin(r), end(r));
    return r;
  };
  auto decryptP = [](const string &s) {
    auto r = s;
    for (auto &&c : r) {
      if (c < '0' || '9' < c) {
        continue;
      }
      c = (c - '0' + 10 - 1) % 10 + '0';
    }
    return r;
  };
  auto decryptM = [](const string &s) {
    auto r = s;
    for (auto &&c : r) {
      if (c < '0' || '9' < c) {
        continue;
      }
      c = (c - '0' + 1) % 10 + '0';
    }
    return r;
  };
  for (int i = 0; i < n; ++i) {
    string order, message;
    cin >> order;
    cin >> message;
    reverse(begin(order), end(order));
    for (auto &&c : order) {
      switch (c) {
        case 'J':
          message = decryptJ(message);
          break;
        case 'C':
          message = decryptC(message);
          break;
        case 'E':
          message = decryptE(message);
          break;
        case 'A':
          message = decryptA(message);
          break;
        case 'P':
          message = decryptP(message);
          break;
        case 'M':
          message = decryptM(message);
          break;
      }
    }
    cout << message << endl;
  }
}
