#include<bits/stdc++.h>
using namespace std;
#define FOR for(int k = 0; k < si2; k++)
#define M(a) ('0'<=a&&a<='9')?(a-'0'+1)%10+'0':a
#define P(a) ('0'<=a&&a<='9')?(a-'0'-1+10)%10+'0':a

int main(){
	int n, si, si2;
	string str, str2, temp;
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>str>>str2;
		si = str.size(); si2 = str2.size();
		for(int j = si-1; j >= 0; j--, str2 = temp){
			temp.clear();
			switch(str[j]){
				case 'C':FOR temp += str2[(k+1)%si2];			break;
				case 'J':FOR temp += str2[(k-1+si2)%si2];		break;
				case 'E':if(si2%2){str2+=str2[si2/2]; 
						 FOR temp += str2[(k+(si2+1)/2)%(si2+1)];}
						 else FOR temp += str2[(k+si2/2)%si2];	break;
				case 'A':FOR temp += str2[si2-k-1];break;
				case 'M':FOR temp += M(str2[k]);				break;
				case 'P':FOR temp += P(str2[k]);				break;
			}
		}
		cout<<str2<<endl;
	}
	return 0;
}
