Read problems statements in Mandarin Chinese  and Russian. 
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :

Ask a volunteer from the audience to write down a list L of N integers. 
    
Ask another volunteer from the audience to provide three integers A, B, C
    
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
    
Close his eyes for a split second and give the output of The Ancient Algorithm on this input. 
    
We all know that The Ancient Algorithm is as follows :

for i from 1 to N do 

if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].

for all number in L[i..N], module them by C.

announce L[i] out loud

end

Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?

------ Constraints: ------ 

1 ≤ T ≤ 100 

1 ≤ N ≤ 1000

0 ≤ L[i] ≤ 10^{18}

0 ≤ A,B ≤ 10^{18}

2 ≤ C ≤ 10^{18}

------ Input ------ 

First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line. 

------ Output ------ 

For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown. 

----- Sample Input 1 ------ 
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------ 
3 3 9
1 2 3 4

Difficulty: MEDIUM_HARD
Tags: Number theory

Data structures

Amortized analysis

Mathematics
Skill types: Amortized analysis

Data structures

Starter code:

Solutions:
t = int(input().strip())
while t:
	n = int(input().strip())
	l = [int(x) for x in input().strip().split()]
	(a, b, c) = (int(x) for x in input().strip().split())
	S = input().strip()
	(s, e, i, x, y) = (0, n - 1, 1, 1, 0)
	for C in S:
		if C == 'R':
			(s, e, i) = (e, s, -i)
		elif C == 'A':
			y += a
			y %= c
		elif C == 'M':
			x *= b
			x %= c
			y *= b
			y %= c
		z = (x * l[s] % c + y % c) % c
		print(z, end=' ')
		s += i
	print()
	t -= 1


from re import L
for _ in range(int(input())):
	mul_val = 1
	add_val = 0
	n = int(input())
	arr = [int(c) for c in input().split()]
	(a, b, c) = [int(c) for c in input().split()]
	s = input()
	for i in range(n):
		if s[i] == 'A':
			add_val += a
		elif s[i] == 'R':
			arr[i:] = arr[i:][::-1]
		elif s[i] == 'M':
			mul_val *= b
			add_val *= b
		arr[i] *= mul_val
		arr[i] += add_val
		arr[i] %= c
	print(*arr)


import sys
T = int(sys.stdin.readline())
while T > 0:
	T -= 1
	N = int(input())
	L = list(map(int, sys.stdin.readline().split()))
	(a, b, c) = map(int, sys.stdin.readline().split())
	a = a % c
	b = b % c
	str = input()
	toadd = 0
	tomul = 1
	rev = 0
	co = 0
	f = 0
	for i in str:
		co += 1
		if i == 'R':
			L.reverse()
		elif i == 'A':
			toadd += a
			toadd = toadd % c
			f = 1
		elif i == 'M':
			tomul *= b
			toadd *= b
			tomul = tomul % c
			toadd = toadd % c
			f = 1
		if rev == 0:
			ans = L[0]
			L.pop(0)
		else:
			ans = L[N - co]
			L.pop(N - co)
		if f == 1:
			ans *= tomul
			ans %= c
			ans += toadd
		ans = ans % c
		print(ans, end=' ')
	print('')


test = int(input())
for t in range(test):
	n = int(input())
	L = list(map(int, input().split()))
	(a, b, c) = map(int, input().split())
	ins = input()
	(sum, m) = (0, 1)
	(f1, l1) = (0, n - 1)
	first = True
	for i in range(n):
		if ins[i] == 'R':
			(f1, l1) = (l1, f1)
			first = not first
		elif ins[i] == 'A':
			sum = (sum % c + a) % c
		else:
			(m, sum) = (m % c * b, sum % c * b)
		L[f1] = (L[f1] * m + sum) % c
		if i == n - 1:
			print(L[f1])
		else:
			print(L[f1], end=' ')
		if first:
			f1 += 1
		else:
			f1 -= 1


import sys
T = int(sys.stdin.readline())
Ans = ''
for t in range(T):
	N = int(sys.stdin.readline())
	L = list(map(int, sys.stdin.readline().split()))
	(A, B, C) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline().split()[0]
	i = 0
	ss = 0
	end = N
	x = 1
	a = 0
	b = 1
	while i != end:
		if s[ss] == 'A':
			a += A
			a %= C
			L[i] = L[i] * b + a
			L[i] %= C
		elif s[ss] == 'R':
			if x == 1:
				(i, end) = (end - 1, i - 1)
				x = -1
			else:
				(i, end) = (end + 1, i + 1)
				x = 1
			L[i] = L[i] * b + a
			L[i] %= C
		elif s[ss] == 'M':
			b *= B
			a *= B
			b %= C
			a %= C
			L[i] = L[i] * b + a
			L[i] %= C
		if ss != N - 1:
			Ans += str(L[i] % C) + ' '
		else:
			Ans += str(L[i] % C)
		i += x
		ss += 1
	Ans += '\n'
sys.stdout.write(Ans)


t = int(input())
j = 0
while t > 0:
	n = int(input())
	(extra, mcnt, rcnt, p1, p2) = (0, 1, 0, -1, n)
	arr = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	s = input()
	for i in s:
		if i == 'R':
			rcnt += 1
		elif i == 'A':
			extra = (extra + a[0]) % a[2]
		else:
			extra = extra * a[1] % a[2]
			mcnt = mcnt * a[1] % a[2]
		if rcnt & 1:
			p2 -= 1
			j = p2
		else:
			p1 += 1
			j = p1
		print((arr[j] * mcnt % a[2] + extra) % a[2], end=' ')
	print()
	t -= 1


t = int(input())
for ti in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	(a, b, c) = [int(x) for x in input().split()]
	s = input()
	multiplier = 1
	addend = 0
	rev = False
	res = []
	i = 0
	j = n - 1
	for ch in s:
		if ch == 'R':
			rev = not rev
		elif ch == 'A':
			addend = (addend + a) % c
		elif ch == 'M':
			multiplier = multiplier * b % c
			addend = addend * b % c
		if rev:
			x = l[j]
			j -= 1
		else:
			x = l[i]
			i += 1
		res.append((x * multiplier + addend) % c)
	print(*res)


def func():
	n = int(input())
	l = map(int, input().split())
	(a, b, c) = map(int, input().split())
	l = list(l)
	s = input()
	st = 0
	en = n - 1
	(x, y) = (1, 0)
	k = 0
	for i in range(0, n):
		if s[i] == 'A':
			y += a
			y = y % c
		elif s[i] == 'M':
			x *= b
			y *= b
			x %= c
			y %= c
		if s[i] == 'R':
			if k == st:
				k = en
			elif k == en:
				k = st
		res = (x * l[k] + y) % c
		if k == st:
			st += 1
			k = st
		elif k == en:
			en -= 1
			k = en
		print(res, end=' ')

def main():
	test = int(input())
	for x in range(0, test):
		func()
		print()
main()


test = int(input())
for t in range(test):
	n = int(input())
	L = list(map(int, input().split()))
	(A, B, C) = map(int, input().split())
	ins = input()
	(add, m) = (0, 1)
	(start, end) = (0, n - 1)
	first = True
	for i in range(n):
		if ins[i] == 'R':
			(start, end) = (end, start)
			first = not first
		elif ins[i] == 'A':
			add = (add % C + A) % C
		else:
			(m, add) = (m % C * B, add % C * B)
		L[start] = (L[start] * m + add) % C
		if i == n - 1:
			print(L[start])
		else:
			print(L[start], end=' ')
		if first:
			start += 1
		else:
			start -= 1


import sys

def pow_mod(x, y, z):
	number = 1
	while y:
		if y & 1:
			number = number * x % z
		y >>= 1
		x = x * x % z
	return number
tokenizedInput = sys.stdin.read().split()
n = int(tokenizedInput[0])
t = 1
for i in range(0, n):
	N = int(tokenizedInput[t])
	t = t + 1
	the_list = list(map(int, tokenizedInput[t:t + N]))
	t = t + N
	A = int(tokenizedInput[t])
	t = t + 1
	B = int(tokenizedInput[t])
	t = t + 1
	C = int(tokenizedInput[t])
	t = t + 1
	S = tokenizedInput[t]
	t = t + 1
	idx = 0
	r = 1
	ansi = 0
	pi = 0
	stri = ''
	for c in range(0, N):
		if S[c] == 'R':
			the_list[idx:N] = reversed(the_list[idx:N])
		idx = idx + 1
		ansi = (ansi + int((the_list[c] - pi) * r)) % C
		pi = the_list[c]
		if S[c] == 'A':
			ansi = (ansi + A) % C
		if S[c] == 'M':
			r = B * r % C
			ansi = ansi * B % C
		stri += str(ansi % C) + ' '
	sys.stdout.write(stri + '\n')


t = int(input())
for cs in range(t):
	n = int(input())
	LS = input().split()
	L = []
	for str in LS:
		L.append(int(str))
	(A1, B1, C1) = input().split()
	A = int(A1)
	B = int(B1)
	C = int(C1)
	S = input()
	i = 0
	j = n - 1
	add = 0
	mul = 1
	flag = 0
	pr = 0
	for ch in S:
		pr = pr + 1
		if ch == 'R':
			flag = 1 - flag
		elif ch == 'A':
			add = (add + A) % C
		elif ch == 'M':
			mul = mul * B % C
			add = add * B % C
		if flag == 1:
			if pr == n:
				print((L[j] * mul + add) % C)
			else:
				print((L[j] * mul + add) % C, end=' ')
			j = j - 1
		else:
			if pr == n:
				print((L[i] * mul + add) % C)
			else:
				print((L[i] * mul + add) % C, end=' ')
			i = i + 1


test = int(input())
for t in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	(x, y, z) = map(int, input().split())
	str = input()
	(a, b, c) = (0, 1, z)
	(strt, end) = (0, n)
	p = 1
	for i in range(n):
		if str[i] == 'R':
			p += 1
			if p % 2 == 0:
				end -= 1
				ans = (l[end] * b + a) % c
			else:
				ans = (l[strt] * b + a) % c
				strt += 1
		elif str[i] == 'A':
			a = (a + x) % c
			if p % 2 == 0:
				end -= 1
				ans = (l[end] * b + a) % c
			else:
				ans = (l[strt] * b + a) % c
				strt += 1
		else:
			(a, b) = (a * y % c, b * y % c)
			if p % 2 == 0:
				end -= 1
				ans = (l[end] * b + a) % c
			else:
				ans = (l[strt] * b + a) % c
				strt += 1
		if i == n - 1:
			print(ans)
		else:
			print(ans, end=' ')


T = int(input())
for i in range(T):
	N = int(input())
	inp = input().split()
	L = [int(x) for x in inp]
	inp2 = input().split()
	A = int(inp2[0])
	B = int(inp2[1])
	C = int(inp2[2])
	S = input()
	pos = 0
	s = 0
	p = 1
	for j in range(N):
		if S[j] == 'R':
			pos = -1 - pos
		elif S[j] == 'A':
			s += A
		elif S[j] == 'M':
			p *= B
			s *= B
		temp = (L[pos] * p % C + s) % C
		L.pop(pos)
		print(temp, end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(a, b, c) = map(int, input().split())
	s = input()
	m = 1
	u = 0
	for j in range(n):
		if s[j] == 'A':
			u = u + a
		elif s[j] == 'R':
			l[j:] = l[j:][::-1]
		elif s[j] == 'M':
			m = m * b
			u = u * b
		l[j] = l[j] * m
		l[j] = l[j] + u
		l[j] = l[j] % c
	print(*l)


itr = int(input())
for j in range(itr):
	N = int(input())
	l = input()
	L = [int(x) for x in l.split()]
	l = input()
	[A, B, C] = [int(x) for x in l.split()]
	S = input()
	for i in range(N):
		if S[i] == 'R':
			y = L[i:]
			L = L[:i] + y[::-1]
		elif S[i] == 'A':
			L[i:] = [x + A for x in L[i:]]
		elif S[i] == 'M':
			L[i:] = [x * B for x in L[i:]]
		L[i:] = [x % C for x in L[i:]]
		print(L[i], end=' ')
	print()


T = int(input())
for t in range(T):
	N = int(input())
	num = input().split()
	num = [int(i) for i in num]
	temp = input().split()
	(A, B, C) = (int(temp[0]), int(temp[1]), int(temp[2]))
	string = input().strip()
	coeff_add = 0
	coeff_mult = 1
	f = N - 1
	s = 0
	k = 0
	for i in string:
		if i == 'R':
			temp = f
			f = s
			s = temp
			result = (num[s] * coeff_mult % C + coeff_add) % C
			if s < f:
				s = s + 1
			else:
				s = s - 1
		elif i == 'A':
			coeff_add = (coeff_add + A) % C
			result = (num[s] * coeff_mult % C + coeff_add) % C
			if s < f:
				s = s + 1
			else:
				s = s - 1
		elif i == 'M':
			coeff_add = coeff_add * B % C
			coeff_mult = coeff_mult * B % C
			result = (num[s] * coeff_mult % C + coeff_add) % C
			if s < f:
				s = s + 1
			else:
				s = s - 1
		k = k + 1
		print(result, end=' ')
	print('')


from sys import stdin
T = int(stdin.readline())
for _ in range(T):
	N = int(stdin.readline())
	L = [int(s) for s in stdin.readline().strip().split()]
	(A, B, C) = (int(s) for s in stdin.readline().strip().split())
	S = stdin.readline().strip()
	(front, back) = (0, N - 1)
	rev = False
	(mul, add) = (1, 0)
	ans = []
	for (idx, op) in enumerate(S):
		if op == 'R':
			rev = not rev
		if rev:
			i = back
			back -= 1
		else:
			i = front
			front += 1
		if op == 'A':
			add = (add + A) % C
		if op == 'M':
			(mul, add) = (mul * B % C, add * B % C)
		ans.append((L[i] * mul + add) % C)
	print(' '.join((str(i) for i in ans)))


import sys
from collections import deque

def main():
	T = int(input())
	for i_th in range(T):
		N = int(input())
		L = deque(input().split())
		(A, B, C) = map(int, input().split())
		S = input()
		mul = 1
		add = 0
		Rflag = True
		for op in S:
			if op == 'R':
				Rflag = not Rflag
			elif op == 'A':
				add = (add + A) % C
			elif op == 'M':
				mul = mul * B % C
				add = add * B % C
			if Rflag:
				v = int(L.popleft())
			else:
				v = int(L.pop())
			sys.stdout.write('%d ' % ((v * mul + add) % C))
		print('')
main()


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().split()))
	(a, b, c) = map(int, input().split())
	s = input()
	(left, right) = (0, n - 1)
	isRev = False
	toAdd = 0
	toMul = 1
	for i in range(len(s)):
		if s[i] == 'R':
			isRev = not isRev
		elif s[i] == 'A':
			toAdd = (toAdd + a) % c
		else:
			toMul = toMul * b % c
			toAdd = toAdd * b % c
		if isRev:
			ele = l[right]
			right -= 1
		else:
			ele = l[left]
			left += 1
		ele = (ele * toMul + toAdd) % c
		print(ele, end=' ')
	print()
	t -= 1


from sys import stdin
T = int(stdin.readline())
for t in range(T):
	N = int(stdin.readline())
	num = [int(i) for i in stdin.readline().split()]
	(a, b, c) = [int(i) for i in stdin.readline().split()]
	string = stdin.readline().strip()
	A = 0
	M = 1
	flag = 0
	start = 0
	end = N - 1
	rev = 0
	for i in range(N):
		if string[i] == 'R':
			rev = 1 if rev == 0 else 0
		elif string[i] == 'A':
			A += a
			flag = 1
		elif string[i] == 'M':
			M = M * b
			A = A * b
			flag = 1
		if rev == 1:
			ans = num[end]
			end = end - 1
		else:
			ans = num[start]
			start = start + 1
		if flag == 1:
			ans = ans * M
			ans += A
		print(ans % c, end=' ')
	print('')


T = int(input())
a = [0 for i in range(0, 1101)]
c = [0 for i in range(0, 1101)]
while T > 0:
	T -= 1
	n = int(input())
	tmp = input().split()
	ba = 0
	bt = 1
	L = 1
	R = n
	for i in range(1, n + 1):
		a[i] = int(tmp[i - 1])
	f = 0
	now = 0
	(add, mul, mod) = [int(x) for x in input().split()]
	s = input()
	for i in range(1, n + 1):
		if s[i - 1] == 'R':
			f = 1 - f
		elif s[i - 1] == 'A':
			ba = (ba + add) % mod
		else:
			ba = ba * mul % mod
			bt = bt * mul % mod
		if f == 1:
			now = R
			R = R - 1
		else:
			now = L
			L = L + 1
		c[i] = (a[now] * bt + ba) % mod
	print(' '.join(map(str, c[1:n + 1])))


def solve():
	n = int(input())
	a = input().split(' ')
	for i in range(n):
		a[i] = int(a[i])
	(A, B, C) = input().split(' ')
	A = int(A)
	B = int(B)
	C = int(C)
	s = input()
	CON = 0
	MULT = 1
	res = ''
	for i in range(n):
		if s[i] == 'R':
			a.reverse()
		if s[i] == 'A':
			CON = (CON + A) % C
		if s[i] == 'M':
			CON = CON * B % C
			MULT = MULT * B % C
		if i > 0:
			res += ' '
		res += str((a[0] * MULT + CON) % C)
		del a[0]
	print(res)
t = int(input())
for i in range(t):
	solve()


tc = int(input())
for t in range(tc):
	n = int(input())
	l = list(map(int, input().split()))
	(a, b, c) = map(int, input().split())
	st = input()
	fuck = True
	temp1 = 0
	temp = 1
	x = 0
	y = n - 1
	for i in range(n):
		if st[i] == 'R':
			(x, y) = (y, x)
			fuck = not fuck
		elif st[i] == 'A':
			temp1 = (temp1 % c + a) % c
		else:
			(temp, temp1) = (temp % c * b, temp1 % c * b)
		l[x] = (l[x] * temp + temp1) % c
		if i == n - 1:
			print(l[x])
		else:
			print(l[x], end=' ')
		if fuck:
			x += 1
		else:
			x -= 1


import sys
l = []
s = ''

def main():
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		head = 0
		flag = 0
		tail = n - 1
		mul_value = 1
		add_value = 0
		l = sys.stdin.readline().strip().split(' ')
		(a, b, c) = sys.stdin.readline().strip().split(' ')
		a = int(a)
		b = int(b)
		c = int(c)
		s = sys.stdin.readline()
		for i in range(n):
			l[i] = int(l[i])
		for i in range(n):
			if s[i] == 'R':
				if flag == 0:
					flag = 1
				else:
					flag = 0
				temp = head
				head = tail
				tail = temp
			elif s[i] == 'A':
				add_value = (add_value + a) % c
			elif s[i] == 'M':
				mul_value = mul_value * b % c
				add_value = add_value * b % c
			l[head] = (l[head] * mul_value % c + add_value) % c
			sys.stdout.write(str(l[head]))
			if i == n - 1:
				sys.stdout.write('\n')
			else:
				sys.stdout.write(' ')
			if flag == 0:
				head += 1
			else:
				head -= 1
main()


T = int(input())
while T > 0:
	N = int(input())
	L = [int(x) for x in input().split(' ')]
	(A, B, C) = (int(x) for x in input().split(' '))
	st = input()
	ans = ''
	forward = True
	fp = 0
	bp = N - 1
	tm = 1
	ta = 0
	for i in range(N):
		if st[i] == 'R':
			forward = not forward
		elif st[i] == 'A':
			ta = (ta + A) % C
		else:
			ta = ta * B % C
			tm = tm * B % C
		if forward:
			ans += str((L[fp] * tm + ta) % C)
			fp += 1
		else:
			ans += str((L[bp] * tm + ta) % C)
			bp -= 1
		if i < N - 1:
			ans += ' '
	print(ans)
	T -= 1


t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	l = [int(x) for x in input().split()]
	inp2 = input().split()
	A = int(inp2[0])
	B = int(inp2[1])
	C = int(inp2[2])
	S = input().strip()
	pos = 0
	s = 0
	p = 1
	for i in range(n):
		if S[i] == 'R':
			pos = -1 - pos
		elif S[i] == 'A':
			s += A
		elif S[i] == 'M':
			p *= B
			s *= B
		res = (l[pos] * p % C + s) % C
		l.pop(pos)
		print(res, end=' ')
	print()


T = int(input())
for _ in range(T):
	N = int(input())
	L = [int(i) for i in input().split()]
	(A, B, C) = [int(i) for i in input().split()]
	S = input()
	direction = 1
	endPos = N
	curPos = 0
	finalValues = []
	coeff = 1
	constant = 0
	for op in S:
		if op == 'A':
			constant = (constant + A) % C
		elif op == 'M':
			constant = constant * B % C
			coeff = coeff * B % C
		elif op == 'R':
			(curPos, endPos) = (endPos - direction, curPos - direction)
			direction *= -1
		L[curPos] = (L[curPos] * coeff + constant) % C
		finalValues.append(L[curPos])
		curPos += direction
	print(*finalValues, sep=' ')


def io(func=str, n=1):

	def nfp():
		ff = str
		try:
			it = iter(func)

			def nf(val):
				nonlocal ff
				ff = next(it, ff)
				return ff(val)
			return type(func)((nf(va) for va in input().split()))
		except TypeError:
			return func(input())
	if type(n) is list:
		return [_(func, x) for x in n]
	else:
		t = lambda : nfp()
		if n == 1:
			return t()
		elif n == 0:
			return [t() for i in range(int(input()))]
		else:
			return [t() for i in range(n)]

def main():
	T = io(int)
	for test in range(T):
		res = []
		N = io(int)
		L = io([int])
		(A, B, C) = io((int, int, int))
		commands = io(list)
		reverse = False
		x = 0
		y = N - 1
		S = 0
		M = 1
		for c in commands:
			if c == 'A':
				S += A
			elif c == 'M':
				M *= B
				S *= B
			elif c == 'R':
				reverse = not reverse
			if reverse:
				res.append((L[y] * M + S) % C)
				y -= 1
			else:
				res.append((L[x] * M + S) % C)
				x += 1
		yield ' '.join([str(r) for r in res])
print('\n'.join([str(res) for res in main()]))


def main():
	t = int(input())
	while t > 0:
		t = t - 1
		n = int(input())
		arr = list(map(int, input().split()))
		(a, b, c) = map(int, input().split())
		s = input()
		a1 = f = 0
		l = m1 = temp = 1
		la = n - 1
		for i in range(n):
			if s[i] == 'A':
				a1 = (a1 + a) % c
			elif s[i] == 'M':
				m1 = m1 * b % c
				a1 = a1 * b % c
			else:
				tmp = f
				f = la
				la = tmp
				if temp == 1:
					temp = 0
				else:
					temp = 1
			arr[f] = (arr[f] * m1 + a1) % c
			if i < n - 1:
				print(arr[f], end=' ')
			else:
				print(arr[f])
			if temp == 1:
				f += 1
			else:
				f -= 1
main()


import sys
data = sys.stdin.read().split()
data.reverse()

def read():
	return int(data.pop())

def readStr():
	return data.pop()
T = read()
for test in range(T):
	n = read()
	A = [read() for i in range(n)]
	(a, b, c) = (read(), read(), read())
	s = readStr()
	ans = []
	(l, r) = (0, n - 1)
	isRev = False
	toAdd = 0
	toMul = 1
	for ch in s:
		if ch == 'R':
			isRev = not isRev
		elif ch == 'A':
			toAdd = (toAdd + a) % c
		else:
			toMul = toMul * b % c
			toAdd = toAdd * b % c
		if isRev:
			ele = A[r]
			r -= 1
		else:
			ele = A[l]
			l += 1
		ele = (ele * toMul + toAdd) % c
		ans.append(str(ele))
	print(' '.join(ans), end='\n')


def sep(s):
	a = []
	b = ''
	for i in list(s):
		if i == ' ':
			a.append(int(b))
			b = ''
		b = b + i
	if b != ' ':
		a.append(int(b))
	return a
t = int(input())
for xx in range(t):
	l = []
	li = []
	n = int(input())
	s = input()
	l = sep(s)
	s = input()
	li = sep(s)
	A = li[0]
	B = li[1]
	C = li[2]
	stri = input()
	list_size = len(l)
	start = 0
	last = n - 1
	added_till_here = 0
	multiplied_till_here = 1
	for i in range(0, n):
		st = start
		en = last
		if stri[i] == 'A':
			added_till_here = (added_till_here + A) % C
		elif stri[i] == 'M':
			multiplied_till_here = multiplied_till_here * B % C
			added_till_here = added_till_here * B % C
		else:
			(start, last) = (last, start)
		l[start] = (l[start] * multiplied_till_here % C + added_till_here) % C
		print(l[start], end=' ')
		if start > last:
			start = start - 1
		else:
			start = start + 1
	print()


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	l = [int(x) for x in input().split()]
	(a, b, c) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	s = input()
	rev = False
	add = 0
	mul = 1
	curr = 0
	start = 0
	end = n - 1
	for i in range(n):
		if s[i] == 'R':
			rev = not rev
		elif s[i] == 'A':
			add += a
			add %= c
		elif s[i] == 'M':
			mul *= b
			mul %= c
			add *= b
			add %= c
		if rev:
			curr = l[end]
			end -= 1
		else:
			curr = l[start]
			start += 1
		print((curr * mul + add) % c, '', end='')
	print('')


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	list = []
	list2 = input().split()
	for i in list2:
		list.append(int(i))
	(a, b, c) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	s = input()
	answer = []
	add = 0
	fact = 1
	i = 0
	j = n - 1
	reverse = False
	for ch in s:
		if ch == 'R':
			reverse = not reverse
		elif ch == 'A':
			add = (add + a) % c
		else:
			fact = fact * b % c
			add = add * b % c
		if reverse:
			answer.append((list[j] % c * fact % c + add) % c)
			j = j - 1
		else:
			answer.append((list[i] % c * fact % c + add) % c)
			i = i + 1
	for x in answer:
		print(x, end=' ')
	print('')


t = int(input())
for i in range(0, t):
	n = int(input())
	arr = [int(j) for j in input().split(' ')]
	(a, b, c) = input().split(' ')
	a = int(a)
	b = int(b)
	c = int(c)
	s = input()
	g = -1
	h = n
	fl = 0
	sum = 0
	pro = 1
	for j in range(0, n):
		if s[j] == 'R' and fl == 0:
			fl = 1
		elif s[j] == 'R' and fl == 1:
			fl = 0
		if fl == 0:
			g += 1
			v = g
		elif fl == 1:
			h -= 1
			v = h
		if s[j] == 'A':
			sum += a
		elif s[j] == 'M':
			sum *= b
			pro *= b
		arr[v] *= pro
		arr[v] += sum
		arr[v] = arr[v] % c
		sum %= c
		pro %= c
		if g > h:
			break
		print(arr[v], end=' ')
	print()


import sys
from operator import mod
t = int(sys.stdin.readline())
for iter in range(t):
	n = int(sys.stdin.readline())
	num_l = list(map(int, sys.stdin.readline().split()))
	(a, b, c) = map(int, sys.stdin.readline().split())
	commands = sys.stdin.readline().rstrip()
	output = ''
	ind = 0
	(to_add, to_mult) = (0, 1)
	for ch in commands:
		if ch == 'R':
			ind = ind % 2 - 1
		elif ch == 'A':
			to_add = mod(to_add + a, c)
		elif ch == 'M':
			to_mult = mod(to_mult * b, c)
			to_add = mod(to_add * b, c)
		next_el = num_l.pop(ind)
		output += str(mod(next_el * to_mult + to_add, c)) + ' '
	print(output.rstrip())


import sys
import operator
from operator import itemgetter
t = int(input())
for i in range(0, t):
	n = int(input())
	L = input().strip().split()
	for j in range(0, n):
		L[j] = int(L[j])
	[A, B, C] = input().strip().split()
	A = int(A)
	B = int(B)
	C = int(C)
	s = input()
	length = 0
	front = 0
	rear = n - 1
	sequence = n * [0]
	for j in range(0, n):
		if s[j] == 'R':
			temp = front
			front = rear
			rear = temp
		sequence[j] = front
		if front == rear:
			break
		if front < rear:
			front += 1
		else:
			front -= 1
	add = 0
	mult = 1
	for j in range(0, n):
		if s[j] == 'A':
			add = (add + A) % C
		elif s[j] == 'M':
			mult = mult * B % C
			add = add * B % C
		ans = L[sequence[j]]
		ans = (ans * mult % C + add) % C
		print(ans, '', end='')
	print()


test = int(input())
for t in range(test):
	n = int(input())
	L = list(map(int, input().split()))
	(A, B, C) = map(int, input().split())
	ins = input()
	(add, m) = (0, 1)
	(fptr, lptr) = (0, n - 1)
	first = True
	ans = []
	for i in range(n):
		if ins[i] == 'R':
			(fptr, lptr) = (lptr, fptr)
			first = not first
		elif ins[i] == 'A':
			add = (add % C + A) % C
		else:
			(m, add) = (m % C * B, add % C * B)
		L[fptr] = (L[fptr] * m + add) % C
		if i == n - 1:
			ans.append(str(L[fptr]))
		else:
			ans.append(str(L[fptr]))
		if first:
			fptr += 1
		else:
			fptr -= 1
	print(' '.join(ans))


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	(a, b, c) = [int(x) for x in input().split()]
	s = input()
	(A, M, left, right, cd) = (0, 1, 0, n - 1, 1)
	for i in range(n):
		if s[i] == 'A':
			A += a
			A %= c
		elif s[i] == 'M':
			M = M * b % c
			A = A * b % c
		elif s[i] == 'R':
			cd *= -1
		if cd == 1:
			x = l[left]
			left += 1
		else:
			x = l[right]
			right -= 1
		print((x * M + A) % c, end=' ')
	print('')


t = int(input())
for q in range(t):
	n = int(input())
	numbers = input()
	x = numbers.split()
	(a, b, c) = map(int, input().split())
	s = input()
	up = 0
	lp = n - 1
	add = 0
	mult = 1
	a = a % c
	b = b % c
	ci = 1
	for i in range(n):
		if s[i] == 'R':
			temp = up
			up = lp
			lp = temp
			ci = -ci
		elif s[i] == 'A':
			add = (add + a) % c
		else:
			add = add * b % c
			mult = mult * b % c
		v = int(x[up])
		v = (v * mult % c + add) % c
		print(v, end=' ')
		up = up + ci
	print(' ')


def process(nums, abc, seq):
	n = len(nums)
	(a, b, c) = abc
	(l, r) = (0, n - 1)
	(x, y) = (1, 0)
	for i in range(n):
		if seq[i] == 'R':
			(l, r) = (r, l)
		elif seq[i] == 'A':
			y = (y + a) % c
		elif seq[i] == 'M':
			x = x * b % c
			y = y * b % c
		nums[l] = (nums[l] * x + y) % c
		if i == n - 1:
			print(nums[l])
		else:
			print(nums[l], end=' ')
		l += 1 if l < r else -1

def solve():
	input()
	nums = [int(x) for x in input().split()]
	abc = tuple((int(x) for x in input().split()))
	seq = input()
	process(nums, abc, seq)
t = int(input())
for i in range(t):
	solve()