#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <cctype>

using namespace std;

int main(){
	int T;
	cin >> T;
	while(T--){
		string a, b;
		cin >> a >> b;
		for(int i = a.size() - 1; i >= 0; --i){
			if(a[i] == 'J'){
				b = b.substr(b.size() - 1) + b.substr(0, b.size() - 1);
			}else if(a[i] == 'C'){
				b = b.substr(1) + b.substr(0, 1);
			}else if(a[i] == 'E'){
				int half = b.size() / 2;
				for(int j = 0; j < half; ++j){
					swap(b[j], b[b.size() - half + j]);
				}
			}else if(a[i] == 'A'){
				reverse(b.begin(), b.end());
			}else if(a[i] == 'P'){
				for(int j = 0; j < b.size(); ++j){
					if(!isdigit(b[j])){ continue; }
					b[j] = (b[j] == '0' ? '9' : (b[j] - 1));
				}
			}else if(a[i] == 'M'){
				for(int j = 0; j < b.size(); ++j){
					if(!isdigit(b[j])){ continue; }
					b[j] = (b[j] == '9' ? '0' : (b[j] + 1));
				}
			}
		}
		cout << b << endl;
	}
	return 0;
}