#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	int n; cin >> n;
	while (n--) {
		string s, t; cin >> t >> s;
		int m = s.length();
		reverse(t.begin(), t.end());
		for (int j = 0; j < t.size(); j++)
			switch (t[j]) {
			case 'J':
				s = s[m - 1] + s.substr(0, m - 1);
				break;
			case 'C':
				s = s.substr(1) + s[0];
				break;
			case 'E':
				if (m % 2 == 0) s = s.substr(m / 2) + s.substr(0, m / 2);
				else s = s.substr(m / 2 + 1) + s[m / 2] + s.substr(0, m / 2);
				break;
			case 'A':
				reverse(s.begin(), s.end());
				break;
			case 'P':
				for (int i = 0; i < m; i++)
					if (s[i] >= '0' && s[i] <= '9')
						s[i] = '0' + (s[i] - '0' + 9) % 10;
				break;
			case 'M':
				for (int i = 0; i < m; i++)
					if (s[i] >= '0' && s[i] <= '9')
						s[i] = '0' + (s[i] - '0' + 1) % 10;
				break;
			}
		cout << s << endl;
	}
}