#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  string order;
  string mess;
  int n;
  int sz;
  cin >> n;
  for (int i1 = 0; i1 < n; i1++) {
    cin >> order;
    cin >> mess;
    reverse(order.begin(), order.end());
    sz = mess.size();
    for (int i2 = 0; i2 < order.size(); i2++) {
      if (order[i2] == 'J') {
        mess = mess[sz - 1] + mess.substr(0, sz - 1);
      } else if (order[i2] == 'C') {
        mess = mess.substr(1, sz - 1) + mess[0];
      } else if (order[i2] == 'E') {
        string l = mess.substr(0, sz / 2);
        string r = mess.substr(sz / 2, sz / 2);
        if (sz % 2 == 0)
          mess = r + l;
        else
          mess = r + mess[sz] + l;
      } else if (order[i2] == 'A') {
        reverse(mess.begin(), mess.end());
      } else if (order[i2] == 'P') {
        for (int i3 = 0; i3 < sz; i3++) {
          if (mess[i3] >= '1' && mess[i3] <= '9')
            mess[i3]--;
          else if (mess[i3] == '0')
            mess[i3] = '9';
        }
      } else {
        for (int i3 = 0; i3 < sz; i3++) {
          if (mess[i3] >= '0' && mess[i3] <= '8')
            mess[i3]++;
          else if (mess[i3] == '9')
            mess[i3] = '0';
        }
      }
    }
    cout << mess << endl;
  }
  return 0;
}
