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

//#define int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

string A(string s) {
	reverse(all(s));
	return s;
}

string J(string s) {
	return s[s.size() - 1] + s.substr(0, s.size() - 1);
}

string M(string s) {
	rep(i, 0, s.size()) {
		if (isdigit(s[i])) {
			s[i] = char((s[i] - '0' + 1 + 10) % 10 + '0');
		}
	}
	return s;
}

string P(string s) {
	rep(i, 0, s.size()) {
		if (isdigit(s[i])) {
			s[i] = char((s[i] - '0' - 1 + 10) % 10 + '0');
		}
	}
	return s;
}

string E(string s) {
	int h = s.size() / 2;
	if (s.size() % 2)return s.substr(h + 1) + s[h] + s.substr(0, h);
	return s.substr(h) + s.substr(0, h);
}

string C(string s) {
	return s.substr(1, s.size()) + s[0];
}

signed main() {
	int n; cin >> n;
	rep(h, 0, n) {
		string o; cin >> o;
		string m; cin >> m;
		rrep(i, 0, o.size()) {
			if (o[i] == 'J')m = J(m);
			if (o[i] == 'C')m = C(m);
			if (o[i] == 'E')m = E(m);
			if (o[i] == 'A')m = A(m);
			if (o[i] == 'P')m = P(m);
			if (o[i] == 'M')m = M(m);
			dump(m);
		}
		cout << m << endl;
	}
	return 0;
}