
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>

using namespace std;


bool solve(){
	string str, ans;
	cin>> str>> ans;
	
	for(int i=0;i<str.size();i++){
		int n = ans.size();
		char c;
		string lstr, rstr;

		switch(str[str.size()-1-i]){
		case 'J':
			c = ans[n-1];
			for(int j=n-1;j>=0;j--) ans[j] = ans[(j+n-1)%n];
			ans[0] = c;
			break;
		case 'C':
			c = ans[0];
			for(int j=0;j<n;j++) ans[j] = ans[(j+1)%n];
			ans[n-1] = c;
			break;
		case 'E':
			if(n%2==1){
				lstr = ans.substr(0,(n-1)/2);
				rstr = ans.substr((n+1)/2);
				rstr += ans[(n-1)/2];
			}else{
				lstr = ans.substr(0,n/2);
				rstr = ans.substr(n/2);
			}
			ans = rstr + lstr;
			break;
		case 'A':
			reverse(ans.begin(), ans.end());
			break;
		case 'P':
			for(int j=0;j<n;j++){
				if('0'<=ans[j] && ans[j]<='9'){
					ans[j] = '0' + ((ans[j]-'0') + 9)%10;
				}
			}
			break;
		case 'M':
			for(int j=0;j<n;j++){
				if('0'<=ans[j] && ans[j]<='9'){
					ans[j] = '0' + ((ans[j]-'0') + 1)%10;
				}
			}
			break;
		default:
			break;
		}
	}

	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	int n;
	cin>> n;
	for(int i=0;i<n;i++) solve();

	return 0;
}

 