#include <bits/stdc++.h>
using std::swap;
void J(char s[], int len) {
  char tmp = s[len - 1];
  for (int i = len - 1; i > 0; i--) {
    s[i] = s[i - 1];
  }
  s[0] = tmp;
}
void C(char s[], int len) {
  char tmp = s[0];
  for (int i = 0; i < len - 1; i++) {
    s[i] = s[i + 1];
  }
  s[len - 1] = tmp;
}
void E(char s[], int len) {
  int mid = len / 2;
  for (int i = 0; i < mid; i++) {
    swap(s[i], s[len - mid + i]);
  }
}
void A(char s[], int len) {
  char tmp = s[len - 1];
  for (int i = 0; i < len - 1 - i; i++) {
    swap(s[i], s[len - 1 - i]);
  }
}
void P(char s[], int len) {
  for (int i = 0; i < len - 1; i++) {
    if ('0' <= s[i] && s[i] <= '9') {
      s[i] = '0' + (s[i] - '0' + 9) % 10;
    }
  }
}
void M(char s[], int len) {
  for (int i = 0; i < len - 1; i++) {
    if ('0' <= s[i] && s[i] <= '9') {
      s[i] = '0' + (s[i] - '0' + 1) % 10;
    }
  }
}
int main() {
  int N;
  scanf("%d", &N);
  while (N--) {
    char s1[50], s2[50];
    scanf("%s %s", s1, s2);
    int len1;
    for (len1 = 0; s1[len1] != '\0'; len1++)
      ;
    int len2;
    for (len2 = 0; s2[len2] != '\0'; len2++)
      ;
    for (int i = len1 - 1; i >= 0; i--) {
      if (s1[i] == 'J') J(s2, len2);
      if (s1[i] == 'C') C(s2, len2);
      if (s1[i] == 'E') E(s2, len2);
      if (s1[i] == 'A') A(s2, len2);
      if (s1[i] == 'P') P(s2, len2);
      if (s1[i] == 'M') M(s2, len2);
    }
    printf("%s\n", s2);
  }
  return 0;
}
