#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int const MOD = 1e9 + 7;
bool sieve[2000000];
int main(void) {
  int n;
  cin >> n;
  for (int k = 0; k < n; ++k) {
    string order, mes;
    cin >> order >> mes;
    reverse(order.begin(), order.end());
    for (int j = 0; j < order.size(); ++j) {
      switch (order[j]) {
        case 'A':
          reverse(mes.begin(), mes.end());
          break;
        case 'J':
          rotate(mes.begin(), mes.begin() + mes.size() - 1, mes.end());
          break;
        case 'C':
          rotate(mes.begin(), mes.begin() + 1, mes.end());
          break;
        case 'E':
          for (int i = 0; i < mes.size() / 2; ++i) {
            swap(mes[i], mes[(mes.size() + 1) / 2 + i]);
          }
          break;
        case 'M':
          for (int i = 0; i < mes.size(); ++i) {
            if (mes[i] >= '0' && mes[i] <= '8') {
              mes[i] = (char)(mes[i] + 1);
              continue;
            }
            if (mes[i] == '9') mes[i] = '0';
          }
          break;
        case 'P':
          for (int i = 0; i < mes.size(); ++i) {
            if (mes[i] >= '1' && mes[i] <= '9') {
              mes[i] = (char)(mes[i] - 1);
              continue;
            }
            if (mes[i] == '0') mes[i] = '9';
          }
          break;
      }
    }
    cout << mes << endl;
  }
  return 0;
}
