#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    string s, t;
    cin >> s >> t;
    reverse(s.begin(), s.end());

    int sz = s.size(), tz = t.size();
    for (int j = 0; j < sz; j++) {
      if (s[j] == 'C') {
        t = t.substr(1, tz - 1) + t[0];
      } else if (s[j] == 'J') {
        t = t[tz - 1] + t.substr(0, tz - 1);
      } else if (s[j] == 'E') {
        string a = t.substr(0, tz / 2), b = t.substr((tz + 1) / 2, tz / 2);
        if (tz & 1) t = t[tz / 2];
        else t = "";
        t = b + t + a;
      } else if (s[j] == 'A') {
        reverse(t.begin(), t.end());
      } else if (s[j] == 'M') {
        for (int k = 0; k < tz; k++) {
          if (!isdigit(t[k])) continue;
          t[k] = (t[k] - '0' + 1) % 10 + '0';
        }
      } else {
        for (int k = 0; k < tz; k++) {
          if (!isdigit(t[k])) continue;
          t[k] = (t[k] - '0' - 1 + 10) % 10 + '0';
        }
      }
    }

    cout << t << endl;
  }

  return 0;
}
