#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;

int main() {
	int n;
	cin >> n;
	while(n--) {
		string order, message;
		cin >> order >> message;
		for(int i=order.length()-1; i>=0; i--) {
			switch(order[i]) {
				case 'J':
					message = message[message.length()-1] + message.substr(0,message.length()-1);
					break;
				case 'C':
					message = message.substr(1,message.length()-1) + message[0];
					break;
				case 'E':
					if(message.length()%2) {
						message = message.substr(message.length()/2+1,message.length()/2) + message[message.length()/2] + message.substr(0,message.length()/2);
					} else {
						message = message.substr(message.length()/2,message.length()/2) + message.substr(0,message.length()/2);
					}
					break;
				case 'A':
					reverse(message.begin(), message.end());
					break;
				case 'P':
					for(int i=0; i<message.length(); i++) {
						if('0'<=message[i] && message[i]<='9') {
							message[i] = ((message[i]-'0'+9)%10)+'0';
						}
					}
					break;
				case 'M':
					for(int i=0; i<message.length(); i++) {
						if('0'<=message[i] && message[i]<='9') {
							message[i] = ((message[i]-'0'+1)%10)+'0';
						}
					}
					break;
			}
		}
		cout << message << endl;
	}
	return 0;
}