#include <bits/stdc++.h>
using namespace std;
void decode(string &s, char c) {
  if (c == 'J') {
    s = s[s.size() - 1] + s.substr(0, s.size() - 1);
  } else if (c == 'C') {
    s = s.substr(1) + s[0];
  } else if (c == 'E') {
    if (s.size() % 2 == 0)
      s = s.substr(s.size() / 2) + s.substr(0, s.size() / 2);
    else
      s = s.substr(s.size() / 2 + 1) + s[s.size() / 2] +
          s.substr(0, s.size() / 2);
  } else if (c == 'A') {
    reverse((s).begin(), (s).end());
  } else if (c == 'P') {
    for (long long i = 0; i < (s.size()); i++) {
      if (s[i] == '0') s[i] = '9';
      if ('1' <= s[i] && s[i] <= '9') s[i]--;
    }
  } else {
    for (long long i = 0; i < (s.size()); i++) {
      if ('0' <= s[i] && s[i] <= '8') s[i]++;
      if (s[i] == '9') s[i] = '0';
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (long long i = 0; i < (n); i++) {
    string s, msg;
    cin >> s >> msg;
    for (long long i = s.size() - 1; i >= 0; i--) {
      decode(msg, s[i]);
    }
    cout << msg << endl;
  }
}
