#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, l;
  string o, k;
  cin >> n;
  while (n--) {
    cin >> o >> k;
    int l = k.size();
    for (int i = o.size() - 1; i >= 0; i--) {
      if (o[i] == 'J')
        k = k[l - 1] + k.substr(0, l - 1);
      else if (o[i] == 'C')
        k = k.substr(1, l - 1) + k[0];
      else if (o[i] == 'E') {
        if (l % 2)
          k = k.substr(l / 2 + 1) + k[l / 2] + k.substr(0, l / 2);
        else
          k = k.substr(l / 2) + k.substr(0, l / 2);
      } else if (o[i] == 'A')
        reverse(k.begin(), k.end());
      else if (o[i] == 'P') {
        for (int j = 0; j < l; j++) {
          if ('1' <= k[j] && k[j] <= '9')
            k[j]--;
          else if (k[j] == '0')
            k[j] = '9';
        }
      } else if (o[i] == 'M') {
        for (int j = 0; j < l; j++) {
          if ('0' <= k[j] && k[j] <= '8')
            k[j]++;
          else if (k[j] == '9')
            k[j] = 0;
        }
      }
    }
    cout << k << endl;
  }
  return 0;
}
