#include <iostream>
#include <deque>
#include <string>
using namespace std;

int main(){
	int n;
	string s,t;
	char c,d;
	deque<char> de,sub;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> s >> t;
		de.clear();
		for(int j = 0;j < t.size();j++){
			de.push_back(t[j]);
		}
		for(int j = s.size()-1;j >= 0;j--){
			//cout << s[j] <<endl;
			if(s[j] == 'C'){
				c = de.front();
				de.pop_front();
				de.push_back(c);
			}
			else if(s[j] == 'J'){
				c = de.back();
				de.pop_back();
				de.push_front(c);
			}
			else if(s[j] == 'A'){
				for(int k = 0;k < de.size()/2;k++){
					swap(de[k],de[de.size()-k-1]);
				}
			}
			else if(s[j] == 'E'){
				if(de.size()%2 == 1){
					for(int k = 0;k < t.size()/2;k++){
						c = de.back();
						de.pop_back();
						sub.push_front(c);
					}
					c = de.back();
					de.pop_back();
					de.push_front(c);
					//cout << c << endl;
					while(!sub.empty()){
						c = sub.back();
						sub.pop_back();
						de.push_front(c);
					}
				}
				else{
					for(int k = 0;k < de.size()/2;k++){
						c = de.front();
						de.pop_front();
						de.push_back(c);
					}
				}
			}
			else if(s[j] == 'M'){
				for(int k = 0;k < de.size();k++){
					if(de[k] >= '0' && de[k] <='8'){
						de[k]++;
					}
					else if(de[k] == '9'){
						de[k] = '0';
					}
				}
			}
			else if(s[j] == 'P'){
				for(int k = 0;k < de.size();k++){
					if(de[k] >= '1' && de[k] <='9'){
						de[k]--;
					}
					else if(de[k] == '0'){
						de[k] = '9';
					}
				}
			}
		}
		for(int j = 0;j < de.size();j++){
			cout << de[j];
			if(j == de.size()-1)cout <<endl;
		}
	}

	return 0;
}