#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    string a, b;
    cin >> b >> a;
    reverse(b.begin(), b.end());
    for (int i = 0; i < b.size(); i++) {
      if (b[i] == 'A')
        reverse(a.begin(), a.end());
      else if (b[i] == 'C') {
        string temp;
        temp = a[0];
        a.erase(a.begin(), a.begin() + 1);
        a += temp;
      } else if (b[i] == 'J') {
        string temp;
        temp = a[a.size() - 1];
        reverse(a.begin(), a.end());
        a.erase(a.begin(), a.begin() + 1);
        reverse(a.begin(), a.end());
        temp += a;
        a.clear();
        a = temp;
      } else if (b[i] == 'E') {
        if (a.size() % 2 == 0) {
          string temp, temp2;
          temp = a.substr(0, a.size() / 2);
          temp2 = a.substr(a.size() / 2, a.size());
          a.clear();
          a = temp2 + temp;
        } else if (a.size() % 2 != 0) {
          string temp, temp2;
          temp = a.substr(0, a.size() / 2 + 1);
          temp2 = a.substr(a.size() / 2 + 1, a.size());
          a.clear();
          a.push_back(temp[temp.size() - 1]);
          reverse(temp.begin(), temp.end());
          temp.erase(temp.begin(), temp.begin() + 1);
          reverse(temp.begin(), temp.end());
          temp2 += a + temp;
          a.clear();
          a = temp2;
        }
      } else if (b[i] == 'P') {
        for (int i = 0; i < a.size(); i++) {
          if (a[i] >= '0' && a[i] <= '9') {
            if (a[i] == '9')
              a[i] = '0';
            else if (a[i] == '0')
              a[i] = '9';
            else
              (int)a[i]--;
          }
        }
      } else if (b[i] == 'M') {
        for (int i = 0; i < a.size(); i++) {
          if (a[i] >= '0' && a[i] <= '9') {
            if ((int)a[i] == '0')
              a[i] = '9';
            else if ((int)a[i] == '9')
              a[i] = '0';
            else
              (int)a[i]++;
          }
        }
      }
    }
    cout << a << endl;
  }
}
