#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(),(a).end()

int main()
{
	int n;
	cin >> n;
	while (n--){
		string m, s;
		cin >> m >> s;
		reverse(all(m));
		for (int i = 0; i < m.size(); i++){
			if (m[i] == 'C'){
				string a(s, 0, 1);
				string b(s, 1, s.size() - 1);
				s = b + a;
			}
			else if (m[i] == 'J'){
				string a(s, 0, s.size() - 1);
				string b(s, s.size() - 1, 1);
				
				//cout << s << " " << a << " " << b << endl;
				
				s = b + a;
			}
			else if (m[i] == 'E'){
				string a(s, 0, s.size() / 2);
				string b(s, s.size() / 2, s.size() % 2);
				string c(s, s.size() / 2 + s.size() % 2, s.size() / 2);
				s = c + b + a;
			}
			else if (m[i] == 'A'){
				reverse(all(s));
			}
			else if (m[i] == 'M'){
				for (int j = 0; j < s.size(); j++){
					if (s[j] >= '0' && s[j] <= '8'){
						s[j]++;
					}
					else if (s[j] == '9'){
						s[j] = '0';
					}
				}
			}
			else if (m[i] == 'P'){
				for (int j = 0; j < s.size(); j++){
					if (s[j] >= '1' && s[j] <= '9'){
						s[j]--;
					}
					else if (s[j] == '0'){
						s[j] = '9';
					}
				}
			}
		}
		
		cout << s << endl;
	}
	
	return 0;
}