#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};
vector<long long> Prime;
void SieveOfEratosthenes(long long e) {
  bool *x = new bool[e];
  for (long long i = 0; i <= e; i++) x[i] = false;
  for (long long i = 2; i <= e; i++) {
    if (x[i])
      ;
    else {
      Prime.push_back(i);
      for (long long l = i; l <= e; l += i) x[l] = true;
    }
  }
  return;
}
int StringToInt(string a) {
  stringstream ss;
  int x;
  ss << a;
  ss >> x;
  return x;
}
string IntToString(int a) {
  stringstream ss;
  string x;
  ss << a;
  ss >> x;
  return x;
}
int GetRandom(int MIN, int MAX) {
  static int flag = 0;
  if (flag == 0) {
    srand((unsigned int)time(NULL));
    flag = 1;
  }
  return MIN + (int)(rand() * (MAX - MIN + 1.0) / (1.0 + RAND_MAX));
}
int in() {
  int x;
  cin >> x;
  return x;
}
void outl(string a) {
  cout << a << endl;
  return;
}
void outl(int a) {
  cout << a << endl;
  return;
}
void out(string a) {
  cout << a;
  return;
}
void out(int a) {
  cout << a;
  return;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    char a[1500];
    string b;
    cin >> a >> b;
    string T;
    for (int i = strlen(a) - 1; i >= 0; i--) {
      switch (a[i]) {
        case 'A':
          reverse(b.begin(), b.end());
          break;
        case 'J':
          T = "";
          T += b[b.size() - 1];
          for (int h = 0; h < b.size() - 1; h++) T += b[h];
          b = T;
          break;
        case 'M':
          for (int l = 0; l < b.size(); l++) {
            if (b[l] >= '0' && b[l] <= '9') {
              if (b[l] == '9')
                b[l] = 0;
              else
                b[l] += 1;
            }
          }
          break;
        case 'P':
          for (int l = 0; l < b.size(); l++) {
            if (b[l] >= '0' && b[l] <= '9') {
              if (b[l] == '0')
                b[l] = 9;
              else
                b[l] -= 1;
            }
          }
          break;
        case 'C':
          b += b[0];
          b.erase(b.begin());
          break;
          break;
        case 'E':
          string r, l;
          int z;
          for (z = 0; z < b.size() / 2; z++) r += b[z];
          if (b.size() % 2) z++;
          for (; z < b.size(); z++) l += b[z];
          if (b.size() % 2) l += b[b.size() / 2];
          b = l + r;
          break;
      }
    }
    cout << b << endl;
  }
}
