#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char messengers[7];
    char message[26];
    cin >> messengers;
    cin >> message;
    int messengersNum = strlen(messengers);
    int messLen = strlen(message);
    for (int j = 0; j < messengersNum; j++) {
      char c = messengers[j];
      if (c == 'J') {
        char tmp = message[0];
        for (int k = 0; k + 1 < messLen; k++) {
          message[k] = message[k + 1];
        }
        message[messLen - 1] = tmp;
      } else if (c == 'C') {
        char tmp = message[messLen - 1];
        for (int k = messLen - 1; k - 1 >= 0; k--) {
          message[k] = message[k - 1];
        }
        message[0] = tmp;
      } else if (c == 'E') {
        if (messLen % 2 == 0) {
          int range = messLen / 2;
          for (int k = 0; k + range < messLen; k++) {
            char tmp = message[k];
            message[k] = message[k + range];
            message[k + range] = tmp;
          }
        } else {
          int range = messLen / 2 + 1;
          for (int k = 0; k + range < messLen; k++) {
            char tmp = message[k];
            message[k] = message[k + range];
            message[k + range] = tmp;
          }
        }
      } else if (c == 'A') {
        for (int k = 0; k < messLen - k; k++) {
          char tmp = message[k];
          message[k] = message[messLen - 1 - k];
          message[messLen - 1 - k] = tmp;
        }
      } else if (c == 'P') {
        for (int k = 0; k < messLen; k++) {
          if (message[k] >= '0' && message[k] <= '9') {
            message[k]++;
            if (message[k] > '9') {
              message[k] = '0';
            }
          }
        }
      } else if (c == 'M') {
        for (int k = 0; k < messLen; k++) {
          if (message[k] >= '0' && message[k] <= '9') {
            message[k]--;
            if (message[k] < '0') {
              message[k] = '9';
            }
          }
        }
      }
    }
    cout << message << endl;
  }
  return 0;
}
