#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int main()
{
  int n;
  cin >> n;
  vector<string> ans;
  while (n--)
  {
    string messengers, message;
    cin >> messengers >> message;
    reverse(messengers.begin(), messengers.end());
    int len = message.size();
    for (int i = 0; i < messengers.size(); ++i)
    {
      if (messengers[i] == 'J')
      {
        rotate(message.rbegin(), message.rbegin() + 1, message.rend());
      }
      else if (messengers[i] == 'C')
      {
        rotate(message.begin(), message.begin() + 1, message.end());
      }
      else if (messengers[i] == 'E')
      {
        string tmp = "";
        tmp += message.substr(len % 2 == 0 ? len / 2 : len / 2 + 1, len / 2);
        if (len % 2 == 1)
          tmp += message[len / 2];
        tmp += message.substr(0, len / 2);
        message = tmp;
      }
      else if (messengers[i] == 'A')
      {
        reverse(message.begin(), message.end());
      }
      else if (messengers[i] == 'P')
      {
        for (int j = 0; j < len; ++j)
        {
          if ('0' <= message[j] && message[j] <= '9')
            message[j] = char((10 + message[j] - 1 - '0') % 10 + '0');
        }
      }
      else if (messengers[i] == 'M')
      {
        for (int j = 0; j < len; ++j)
        {
          if ('0' <= message[j] && message[j] <= '9')
            message[j] = char((message[j] + 1 - '0') % 10 + '0');
        }
      }
    }
    ans.push_back(message);
  }
  for (auto &e : ans)
    cout << e << endl;

  return 0;
}

