#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n; cin >> n;
  while(n--) {
    string MSer, MS; cin >> MSer >> MS;
    int len = MS.length();
    for(int i = (int)MSer.length() - 1; i >= 0; i--) {
      if(MSer[i] == 'J') {
        for(int i = len - 1; i > 0; i--) {
          swap(MS[i-1], MS[i]);
        }
      }
      if(MSer[i] == 'C') {
        FOR(i,1,len) {
          swap(MS[i-1], MS[i]);
        }
      }
      if(MSer[i] == 'E') {
        string ms1 = "", ms2 = "";
        FOR(i,0,len) {
          if(i < len / 2) {
            ms1 += MS[i];
          } else if(len % 2 == 1 && i == len / 2) {
            ms1 = MS[i] + ms1;
          } else {
            ms2 += MS[i];
          }
        }
        MS = ms2 + ms1;
      }
      if(MSer[i] == 'A') {
        reverse(MS.begin(), MS.end());
      }
      if(MSer[i] == 'P') {
        FOR(i,0,len) {
          if(isdigit(MS[i])){
            int x = MS[i] - '0';
            x = (x - 1 + 10) % 10;
            char y = x + '0';
            MS[i] = y;
          }
        }
      }
      if(MSer[i] == 'M') {
        FOR(i,0,len) {
          if(isdigit(MS[i])){
            int x = MS[i] - '0';
            x = (x + 1) % 10;
            char y = x + '0';
            MS[i] = y;
          }
        }
      }
    }
    cout << MS << endl;
  }
  return 0;
}