#include <bits/stdc++.h>
template <class T, class S>
void cmin(T &a, const S &b) {
  if (a > b) a = b;
}
template <class T, class S>
void cmax(T &a, const S &b) {
  if (a < b) a = b;
}
using namespace std;
string solve(string s, char order) {
  if (order == 'C') {
    char hoge = s[0];
    s.erase(s.begin());
    s.push_back(hoge);
    return s;
  }
  if (order == 'J') {
    char hoge = s[s.size() - 1];
    s.insert(s.begin(), hoge);
    s.erase(s.end() - 1);
    return s;
  }
  if (order == 'E') {
    string hoge;
    if (s.size() % 2) hoge.push_back(s[(s.size() - 1) / 2]);
    for (int i = 0; i <= (s.size() - 1) / 2; i++) {
      if (i == (s.size() - 1) / 2 && s.size() % 2) continue;
      hoge.push_back(s[i]);
    }
    string gotiusa;
    for (int i = (int)(s.size() - 1) / 2 + 1; i < s.size(); i++)
      gotiusa.push_back(s[i]);
    return (gotiusa + hoge);
  }
  if (order == 'A') {
    reverse(s.begin(), s.end());
    return s;
  }
  if (order == 'P') {
    for (int i = 0; i < s.size(); i++) {
      if ('0' <= s[i] && s[i] <= '9') {
        if (s[i] == '9')
          s[i] = '0';
        else {
          string hoge = to_string(s[i] - '1');
          s[i] = hoge[0];
        }
      }
    }
    return s;
  }
  if (order == 'M') {
    for (int i = 0; i < s.size(); i++) {
      if ('0' <= s[i] && s[i] <= '9') {
        if (s[i] == '0')
          s[i] = '9';
        else {
          string hoge = to_string(s[i] - '0' + 1);
          s[i] = hoge[0];
        }
      }
    }
    return s;
  }
  return s;
}
int main() {
  int gotiusa;
  cin >> gotiusa;
  for (int _ = 0; _ < gotiusa; _++) {
    string key, s;
    cin >> key;
    cin >> s;
    for (int i = (int)key.size() - 1; i >= 0; i--) {
      s = solve(s, key[i]);
    }
    cout << s << endl;
  }
}
