#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
long long int power(long long int x, long long int n, long long int M) {
  long long int tmp = 1;
  if (n > 0) {
    tmp = power(x, n / 2, M);
    if (n % 2 == 0)
      tmp = (tmp * tmp) % M;
    else
      tmp = (((tmp * tmp) % M) * x) % M;
  }
  return tmp;
}
long long int N, M, K, Q, W, H, L, R;
long long int ans;
int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N; i++) {
    string a, b;
    cin >> a >> b;
    for (auto j = a.rbegin(); j != a.rend(); ++j) {
      if (*j == 'J') {
        char c;
        c = b.back();
        b.pop_back();
        b = c + b;
      }
      if (*j == 'C') {
        char c;
        c = b[0];
        for (int k = 0; k < b.size() - 1; k++) {
          b[k] = b[k + 1];
        }
        b[b.size() - 1] = c;
      }
      if (*j == 'E') {
        string s, t;
        for (int k = 0; k < b.size() / 2; k++) {
          s += b[k];
        }
        for (int k = (b.size() + 1) / 2; k < b.size(); k++) {
          t += b[k];
        }
        for (int k = 0; k < b.size() / 2; k++) {
          b[k] = t[k];
        }
        for (int k = (b.size() + 1) / 2; k < b.size(); k++) {
          b[k] = s[k - (b.size() + 1) / 2];
        }
      }
      if (*j == 'A') {
        for (int k = 0; k < b.size() / 2; k++) swap(b[k], b[b.size() - 1 - k]);
      }
      if (*j == 'M') {
        for (auto k : b) {
          if (k >= '0' && k <= '9') {
            k = char(k - 1);
            if (k == char('0' - 1)) k = '9';
          }
        }
      }
      if (*j == 'P') {
        for (auto k : b) {
          if (k >= '0' && k <= '9') {
            k = char(k + 1);
            if (k == char('9' + 1)) k = '0';
          }
        }
      }
    }
    cout << b << endl;
  }
  return 0;
}
