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

//#define int long long
#define DBG 1
#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 MrJ(string s) { return s.substr(1) + s.front(); }
string MissC(string s) { return s.back() + s.substr(0, s.size() - 1); }
string MrE(string s) {
	string res;
	if (s.size() % 2) {
		res = s[s.size() / 2];
		res += s.substr(0, s.size() / 2);
		res = s.substr(s.size() / 2 + 1)+res;
	}
	else {
		res = s.substr(s.size() / 2) + s.substr(0, s.size() / 2);
	}
	return res;
}
string MrA(string s) { string res = s; reverse(res.begin(), res.end()); return res; }
string DrP(string s) {
	rep(i, 0, s.size()) {
		if (isdigit(s[i])) {
			if (s[i] == '9')s[i] = '0';
			else s[i]++;
		}
	}
	return s;
}
string MrM(string s) {
	rep(i, 0, s.size()) {
		if (isdigit(s[i])) {
			if (s[i] == '0')s[i] = '9';
			else s[i]--;
		}
	}
	return s;
}
signed main() {
	int n; cin >> n;
	while (n--) {
		string order, king;
		cin >> order >> king;
		rrep(i, 0, order.size()) {
			switch (order[i])
			{
			case 'J':king = MissC(king); break;
			case 'C':king = MrJ(king); break;
			case 'E':king = MrE(king); break;
			case 'A':king = MrA(king); break;
			case 'P':king = MrM(king); break;
			case 'M':king = DrP(king); break;
			default:
				break;
			}
		}

		cout << king << endl;
	}
	return 0;
}