#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e9+9
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n;
string s, ans;

int main(){
	cin >> n;
	REP(k, n){
		cin >> s >> ans;
		reverse(ALL(s));
		REP(i,s.size()){
			if (s[i] == 'J'){
				ans = ans[ans.size() - 1] + ans.substr(0, ans.size() - 1);
			}
			else if (s[i] == 'C'){
				ans = ans.substr(1, ans.size() - 1) + ans[0];
			}
			else if (s[i] == 'E'){
				int num = ans.size() / 2;
				if (ans.size() % 2 == 1){
					ans = ans.substr(num + 1) + ans[num] + ans.substr(0, num);
				}
				else{
					ans = ans.substr(num) + ans.substr(0, num);
				}
			}
			else if (s[i] == 'A'){
				reverse(ALL(ans));
			}
			else if (s[i] == 'P'){
				REP(j, ans.size()){
					if (ans[j] >= '1'&&ans[j] <= '9'){
						ans[j] = (char)(ans[j] - 1);
					}
					else if (ans[j] == '0'){
						ans[j] = '9';
					}
				}
			}
			else{
				REP(j, ans.size()){
					if (ans[j] >= '0'&&ans[j] <= '8'){
						ans[j] = (char)(ans[j] + 1);
					}
					else if (ans[j] == '9'){
						ans[j] = '0';
					}
				}
			}
		}
		cout << ans << endl;
	}
}