#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE 

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

int main(){
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	while(n--){
		string ppl, mes;
		cin >> ppl >> mes;

		reverse(ALL(ppl));
		for (auto ch : ppl){
			string nxt;
			int m;
			switch (ch){
			case 'J':
				nxt = mes.back() + mes.substr(0, mes.size() - 1);
				break;
			case 'C':
				nxt = mes.substr(1) + mes[0];
				break;
			case 'E':
				m = mes.size() / 2;
				if (mes.size() & 1){
					nxt = mes.substr(m+1) + mes[m] + mes.substr(0, m);
				}
				else{
					nxt = mes.substr(m) + mes.substr(0, m);
				}
				break;
			case 'A':
				nxt = string(REV(mes));
				break;
			case 'M':
				for (auto c : mes){
					if ('0' <= c && c <= '8') ++c;
					else if (c == '9') c = '0';
					nxt += c;
				}
				break;
			case 'P':
				for (auto c : mes){
					if ('1' <= c && c <= '9') --c;
					else if (c == '0') c = '9';
					nxt += c;
				}
				break;
			}
			mes = nxt;
		}
		cout << mes << endl;
	}


	return 0;
}