#include <bits/stdc++.h>
using namespace std;
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
string MrJ(string s) { return s.substr(1) + s.front(); }
string MissC(string s) { return s.back() + s.substr(0, s.size() - 1); }
string MrE(string s) {
  string res;
  if (s.size() % 2) {
    res = s[s.size() / 2];
    res += s.substr(0, s.size() / 2);
    res = s.substr(s.size() / 2 + 1) + res;
  } else {
    res = s.substr(s.size() / 2) + s.substr(0, s.size() / 2);
  }
  return res;
}
string MrA(string s) {
  string res = s;
  reverse(res.begin(), res.end());
  return res;
}
string DrP(string s) {
  for (int i = (0); i < (s.size()); i++) {
    if (isdigit(s[i])) {
      if (s[i] == '9')
        s[i] = '0';
      else
        s[i]++;
    }
  }
  return s;
}
string MrM(string s) {
  for (int i = (0); i < (s.size()); i++) {
    if (isdigit(s[i])) {
      if (s[i] == '0')
        s[i] = '9';
      else
        s[i]--;
    }
  }
  return s;
}
signed main() {
  int n;
  cin >> n;
  while (n--) {
    string order, king;
    cin >> order >> king;
    for (int i = (order.size()) - 1; i >= (0); i--) {
      switch (order[i]) {
        case 'J':
          king = MissC(king);
          break;
        case 'C':
          king = MrJ(king);
          break;
        case 'E':
          king = MrE(king);
          break;
        case 'A':
          king = MrA(king);
          break;
        case 'P':
          king = MrM(king);
          break;
        case 'M':
          king = DrP(king);
          break;
        default:
          break;
      }
    }
    cout << king << endl;
  }
  return 0;
}
