#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main(){
  int q;
  cin >> q;
  rep(t,q){
    string m;
    string s;
    cin >> m >> s;
    reverse(all(m));
    rep(i,m.length()){
      if(m[i]=='J'){
        char tmp = s[s.length()-1];
        perr(j,1,s.length()){
          s[j]=s[j-1];
        }
        s[0]=tmp;
      }else if(m[i]=='C'){
        char tmp = s[0];
        rep(j,s.length()-1){
          s[j]=s[j+1];
        }
        s[s.length()-1]=tmp;
      }else if(m[i]=='E'){
        if(s.length()%2){
          s = s.substr(s.length()/2+1,s.length()/2)+s.substr(s.length()/2,1)+s.substr(0,s.length()/2);
        }else{
          s = s.substr(s.length()/2,s.length()/2)+s.substr(0,s.length()/2);
        }
      }else if(m[i]=='A'){
        reverse(all(s));
      }else if(m[i]=='P'){
        rep(j,s.length()){
          if('0'<=s[j]&&s[j]<='9'){
            s[j]=((s[j]-'0')+9)%10+'0';
          }
        }
      }else{
        rep(j,s.length()){
          if('0'<=s[j]&&s[j]<='9'){
            s[j]=((s[j]-'0')+1)%10+'0';
          }
        }
      }
    }
    cout << s << endl;
  }
  return 0;
}

