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

string j(string s){
	string ret;
	ret += s[s.size()-1];
	for(int unsigned i = 0;i<s.size()-1;i++){
		ret += s[i];
	}
	return ret;
}
string c(string s){
	string ret;
	for(int unsigned i = 1;i<s.size();i++){
		ret += s[i];
	}
	ret += s[0];
	return ret;
}
string e(string s){
	string ret;
	int size = s.size()/2;
	for(int unsigned i = s.size()-size;i<s.size();i++){
		ret += s[i];
	}
	if(s.size()%2){
		ret += s[s.size()/2];
	}
	for(int unsigned i = 0;i<s.size()/2;i++){
		ret += s[i];
	}
	return ret;
}
string a(string s){
	//reverse
	return string(s.rbegin(),s.rend());
}
string p(string s){
	//increments
	string ret;
	for(int unsigned i = 0;i<s.size();i++){
		if(isdigit(s[i])) {
			ret += (char)((s[i] - '0' + 9)%10 + '0');
		} else {
			ret += s[i];
		}
	}
	return ret;
}
string m(string s){
	//decrements
	string ret;
	for(int unsigned i = 0;i<s.size();i++){
		if(isdigit(s[i])) {
			ret += (char)((s[i] - '0' + 1)%10 + '0');
		} else {
			ret += s[i];
		}
	}
	return ret;
}

int main(){
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		string messengers;
		cin >> messengers;
		string message;
		cin >> message;
		for(int i = (signed)messengers.size()-1;i>=0;i--){
			if(messengers[i] == 'J') message=j(message);
			if(messengers[i] == 'C') message=c(message);
			if(messengers[i] == 'E') message=e(message);
			if(messengers[i] == 'A') message=a(message);
			if(messengers[i] == 'P') message=p(message);
			if(messengers[i] == 'M') message=m(message);
		}
		cout << message << endl;
	}
	return 0;
}