#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        n = input()
        for i in range(n):
            o = raw_input()
            m = raw_input()
            for c in reversed(o):
                if c == 'J':
                    m = m[-1] + m[:-1]
                elif c == 'C':
                    m = m[1:] + m[0]
                elif c == 'E':
                    if len(m) % 2 == 0:
                        m = m[len(m)/2:] + m[:len(m)/2]
                    else:
                        m = m[len(m)/2+1:] + m[len(m)/2] + m[:len(m)/2]
                elif c == 'A':
                    m = m[::-1]
                elif c == 'P':
                    tmp = ''
                    for j in range(len(m)):
                        if m[j].isdigit():
                            d = int(m[j]) - 1
                            if d < 0:
                                d += 10
                            tmp += str(d)
                        else:
                            tmp += m[j]
                    m = tmp
                elif c == 'M':
                    tmp = ''
                    for j in range(len(m)):
                        if m[j].isdigit():
                            d = int(m[j]) + 1
                            if d > 9:
                                d -= 10
                            tmp += str(d)
                        else:
                            tmp += m[j]
                    m = tmp
            print m
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()