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

int main(){
	int N;
	cin >> N;
	while(N--){
		string order, mess;
		cin >> order >> mess;
		for(int i = order.size() - 1; i >= 0; i--){
			char c;
			switch(order[i]){
				case 'J':
					c = mess[mess.size() - 1];
					for(int j = mess.size() - 1; j > 0; j--){
						mess[j] = mess[j - 1];
					}
					mess[0] = c;
					break;
				case 'C':
					c = mess[0];
					for(int j = 1; j < mess.size(); j++){
						mess[j - 1] = mess[j];
					}
					mess[mess.size() - 1] = c;
					break;
				case 'E':
					if(mess.size() % 2){
						mess = mess.substr(mess.size() / 2 + 1) + mess[mess.size() / 2] + mess.substr(0, mess.size() / 2);
					}else{
						mess = mess.substr(mess.size() / 2) + mess.substr(0, mess.size() / 2) ;
					}
					break;

				case 'A':
					reverse(mess.begin(), mess.end());
					break;
				case 'P':
					for(int i = 0; i < mess.size(); i++){
						c = mess[i];
						if('0' <= c && c <= '9'){
							mess[i] = (c - '0' + 9) % 10 + '0';
						}
					}
					break;
				case 'M':
					for(int i = 0; i < mess.size(); i++){
						c = mess[i];
						if('0' <= c && c <= '9'){
							mess[i] = (c - '0' + 1) % 10 + '0';
						}
					}
					break;
			}
		}
		cout << mess << endl;
	}

}