#include <bits/stdc++.h>
using namespace std;
void solve() {
  string order, message;
  cin >> order >> message;
  for (int i = order.size() - 1; i >= 0; i--) {
    switch (order[i]) {
      case 'J':
        if (message.size() >= 2) {
          message = (*message.rbegin()) + message.substr(0, message.size() - 1);
        }
        break;
      case 'C':
        if (message.size() >= 2) {
          message = message.substr(1) + message[0];
        }
        break;
      case 'E':
        if (message.size() >= 2) {
          int pos = message.size() / 2;
          message = message.substr(pos) + message.substr(0, pos);
        }
        break;
      case 'A':
        message = string(message.rbegin(), message.rend());
        break;
      case 'P':
        for (unsigned j = 0; j < message.size(); j++) {
          if (isdigit(message[j])) {
            if (message[j] == '0') {
              message[j] = '9';
            } else {
              message[j]--;
            }
          }
        }
        break;
      case 'M':
        for (unsigned j = 0; j < message.size(); j++) {
          if (isdigit(message[j])) {
            if (message[j] == '9') {
              message[j] = '0';
            } else {
              message[j]++;
            }
          }
        }
        break;
    }
  }
  cout << message << endl;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    solve();
  }
  return 0;
}
