Luca has a cypher made up of a sequence of $n$ wheels, each with a digit $a_i$ written on it. On the $i$-th wheel, he made $b_i$ moves. Each move is one of two types:

up move (denoted by ${U}$): it increases the $i$-th digit by $1$. After applying the up move on $9$, it becomes $0$.

down move (denoted by ${D}$): it decreases the $i$-th digit by $1$. After applying the down move on $0$, it becomes $9$.

Example for $n=4$. The current sequence is 0 0 0 0.

Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.


-----Input-----

The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 100$) — the number of wheels.

The second line contains $n$ integers $a_i$ ($0 \leq a_i \leq 9$) — the digit shown on the $i$-th wheel after all moves have been performed.

Then $n$ lines follow, the $i$-th of which contains the integer $b_i$ ($1 \leq b_i \leq 10$) and $b_i$ characters that are either ${U}$ or ${D}$ — the number of moves performed on the $i$-th wheel, and the moves performed. ${U}$ and ${D}$ represent an up move and a down move respectively.


-----Output-----

For each test case, output $n$ space-separated digits  — the initial sequence of the cypher.


-----Examples-----

Input
3
3
9 3 1
3 DDD
4 UDUU
2 DU
2
0 9
9 DDDDDDDDD
9 UUUUUUUUU
5
0 5 9 8 3
10 UUUUUUUUUU
3 UUD
8 UUDUUDDD
10 UUDUUDUDDU
4 UUUU
Output
2 1 1 
9 0 
0 4 9 6 9


-----Note-----

In the first test case, we can prove that initial sequence was $[2,1,1]$. In that case, the following moves were performed:

On the first wheel: $2 \xrightarrow[{D}]{} 1 \xrightarrow[{D}]{} 0 \xrightarrow[{D}]{} 9$.

On the second wheel: $1 \xrightarrow[{U}]{} 2 \xrightarrow[{D}]{} 1 \xrightarrow[{U}]{} 2 \xrightarrow[{U}]{} 3$.

On the third wheel: $1 \xrightarrow[{D}]{} 0 \xrightarrow[{U}]{} 1$.

The final sequence was $[9,3,1]$, which matches the input.

Source: codeforces | Difficulty: EASY
Tags: String algorithms

Implementation

Complete search


Solutions:
def main():
	i = int(input())
	for _ in range(i):
		n = int(input().strip('\r\n'))
		arr = [int(x) for x in input().split()]
		for idx in range(n):
			s = input().strip('\r\n').split()[1]
			ups = s.count('D') - s.count('U')
			arr[idx] = (arr[idx] + ups) % 10
		print(' '.join([str(x) for x in arr]))
main()


def solve():
	N = int(input())
	a = list(map(int, input().split()))
	for i in range(N):
		(_, s) = input().split()
		s = reversed(s)
		for x in s:
			if x == 'D':
				a[i] = (a[i] + 1 + 10) % 10
			else:
				a[i] = (a[i] - 1 + 10) % 10
	print(*a)
T = int(input())
for _ in range(T):
	solve()


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		(s, t) = input().split()
		a[i] = (a[i] + t.count('D') - t.count('U')) % 10
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	for i in range(n):
		(x, s) = input().split()
		lst[i] += s.count('D')
		lst[i] %= 10
		lst[i] -= s.count('U')
		lst[i] = (lst[i] % 10 + 10) % 10
	print(*lst)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	for i in range(n):
		(length, s) = list(input().split())
		length = int(length)
		move = length - 2 * s.count('U')
		lis[i] += move
		lis[i] %= 10
	print(*lis)


import math
ints = lambda : list(map(int, input().split()))
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
mx = 10 ** 5
inp = lambda : int(input())

def cipher():
	n = inp()
	a = ints()
	arr = []
	for i in range(n):
		(nmoves, moves) = input().split()
		add = moves.count('D') - moves.count('U')
		arr.append((a[i] + add) % 10)
	print(*arr)
for _ in range(inp()):
	cipher()


fptr = open(0)
for _ in range(int(next(fptr))):
	n = int(next(fptr))
	a = list(map(int, next(fptr).split()))
	for i in range(n):
		(n1, s) = next(fptr).split()
		count = 0
		count += s.count('D')
		count -= s.count('U')
		a[i] += count
		if a[i] > 9 or a[i] < 0:
			a[i] %= 10
	for i in a:
		print(i, end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	finals = [int(w) for w in input().split()]
	for j in range(n):
		dir = input().split()[1]
		finals[j] = str((finals[j] + dir.count('D') - dir.count('U')) % 10)
	print(' '.join(finals))


for t in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	for j in range(n):
		B = input().split()[1]
		a = B.count('U')
		b = B.count('D')
		if a == b:
			A[j] = str(A[j])
		elif a > b:
			x = a - b
			A[j] = str((10 - x + A[j]) % 10)
		else:
			x = b - a
			A[j] = str((x + A[j]) % 10)
	print(' '.join(A))


t = int(input())
for i in range(t):
	n = int(input())
	last_numbers = list(map(int, input().split()))
	first_numbers = list()
	for j in range(n):
		(b, b_str) = input().split()
		b = int(b)
		U_number = len(b_str.replace('D', ''))
		D_number = b - U_number
		first_numbers.append((last_numbers[j] - U_number + D_number) % 10)
	print(*first_numbers)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		p = input().split()
		a[i] = (a[i] + p[1].count('D') - p[1].count('U')) % 10
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		(x, y) = input().split()
		D = y.count('D')
		U = y.count('U')
		b.append(D - U)
	for (x, y) in zip(a, b):
		print((10 + x + y) % 10, end=' ')
	print()


n = int(input())
for _ in range(n):
	_ = input()
	digits = input().strip().split()
	for i in range(len(digits)):
		line = input().split()[1]
		v = 0
		for c in line:
			if c == 'D':
				v += 1
			else:
				v -= 1
		digits[i] = str((int(digits[i]) + v) % 10)
	print(' '.join(digits))


for i in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	answer = []
	for j in range(n):
		xs = input().split()
		com = xs[1]
		k = int(xs[0])
		d = com.count('D')
		answer.append((num[j] + 2 * d - k) % 10)
	print(*answer)


for _ in range(int(input())):
	n = int(input())
	f = list(map(int, input().split()))
	v = []
	for i in range(n):
		l = input().split()
		a = l[1]
		u = a.count('U')
		d = a.count('D')
		v.append((f[i] + d - u) % 10)
	print(*v)


t = int(input())
for b in range(t):
	wheels = int(input())
	combo = [int(x) for x in input().split()]
	ans = ''
	for w in range(wheels):
		move = input()
		uMoves = move.count('U')
		dMoves = move.count('D')
		res = combo[w] + uMoves * -1 + dMoves
		res = res % 10
		ans += str(res) + ' '
	print(ans[:-1])


for i in range(int(input())):
	n = int(input())
	lst = [int(x) for x in input().split()]
	ans = []
	for j in range(n):
		(wq, s) = input().split()
		ans.append(str((lst[j] + s.count('D') - s.count('U')) % 10))
	print(' '.join(ans))


n0 = int(input())
for ncase in range(1, n0 + 1):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	for i in range(n):
		(bi, b) = input().split(' ')
		t = 0
		for j in b:
			if j == 'D':
				t += 1
			else:
				t -= 1
		a[i] = (a[i] + t) % 10
	print(' '.join([str(i) for i in a]))


def decrement(n):
	if n == 0:
		return 9
	return n - 1

def increment(n):
	if n == 9:
		return 0
	return n + 1
t = int(input())
for i in range(t):
	n = int(input())
	numbers = list(map(int, input().split()))
	for j in range(n):
		(m, moves) = input().split()
		for ch in moves:
			if ch == 'D':
				numbers[j] = increment(numbers[j])
			else:
				numbers[j] = decrement(numbers[j])
	numbers = map(str, numbers)
	print(' '.join(numbers))


for _ in range(int(input())):
	n = int(input())
	lst = list(map(lambda x: int(x), input().split()))
	for i in range(n):
		(counter, povoroti) = input().split()
		Dcount = povoroti.count('D')
		Ucount = povoroti.count('U')
		Newcount = Dcount - Ucount
		oldznak = lst[i]
		newznak = oldznak + Newcount
		while newznak > 9:
			newznak -= 10
		while newznak < 0:
			newznak += 10
		lst[i] = str(newznak)
	print(' '.join(lst))


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		values = [int(j) for j in input().split(' ')]
		for j in range(n):
			(length, sequence) = [k for k in input().split(' ')]
			action = 0
			for val in sequence:
				if val == 'D':
					action += 1
				else:
					action -= 1
			action %= 10
			values[j] += action
			values[j] %= 10
		print(' '.join((str(j) for j in values)))
main()


def nrs(nr1, nr2):
	if nr1 + nr2 > 9:
		return nr1 + nr2 - 10
	elif nr1 + nr2 < 0:
		return nr1 + nr2 + 10
	else:
		return nr1 + nr2
for i in range(int(input())):
	nr = int(input())
	dials = list(map(int, input().split(' ')))
	m = []
	for j in range(nr):
		(nr_of_moves, moves) = input().split(' ')
		(nr_of_moves, moves) = (int(nr_of_moves), list(moves))
		moves = sum(list(map(lambda x: -1 if x == 'U' else 1, moves)))
		m.append(moves)
	ans = [nrs(x, y) for (x, y) in zip(dials, m)]
	print(' '.join([str(x) for x in ans]))


for _ in range(int(input())):
	n = int(input())
	arr = input().split()
	for i in range(n):
		(m, s) = input().split()
		(u, d) = (s.count('U'), s.count('D'))
		arr[i] = str((int(arr[i]) + d - u) % 10)
	print(' '.join(arr))


t = int(input())

def kamla(num, actionx):
	for action in actionx:
		if action == 'D':
			if num == 9:
				num = 0
			else:
				num += 1
		elif num == 0:
			num = 9
		else:
			num -= 1
	return num
for i in range(t):
	result = []
	clicks = []
	n = int(input())
	wheels = list(map(int, input().split()))
	for k in range(n):
		s = input()
		s = s.split(' ')
		clicks.append(s)
	ind = 0
	while ind < n:
		x = kamla(wheels[ind], clicks[ind][1])
		result.append(x)
		ind += 1
	result = ' '.join(map(str, result))
	print(result)


def fun(seq_list, final_list):
	empty_list = []
	for i in range(len(seq_list)):
		count_of_d_plus = seq_list[i].upper().count('D')
		count_of_u_min = seq_list[i].upper().count('U')
		empty_list.append((int(final_list[i]) + count_of_d_plus - count_of_u_min) % 10)
	print(' '.join((str(e) for e in empty_list)))
for i in range(int(input())):
	my_input = int(input())
	final_list = input().split()
	seq_list = []
	for i in range(my_input):
		my_list = input().split()
		seq_list.append(my_list[1])
	fun(seq_list, final_list)


T = int(input())
t = {'D': -1, 'U': 1}
for _ in range(T):
	rings = int(input())
	values = list(map(int, input().split()))
	for i in range(rings):
		(times, dirs) = input().split()
		times = int(times)
		values[i] = (values[i] + (dirs.count('U') - dirs.count('D')) * -1) % 10
	print(' '.join(map(str, values)))


t = int(input())
for i in range(1, t + 1):
	n = int(input())
	a = input()
	o = ''
	for j in range(n):
		s = input()
		o += str((int(a[2 * j]) - s.count('U') + s.count('D')) % 10) + ' '
	print(o)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		pattern = input()
		d_count = pattern[2:].count('D')
		u_count = pattern[2:].count('U')
		ans = d_count - u_count
		ans = ans + a[i]
		b.append(ans % 10)
	print(*b)


t = int(input())
for r in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = ''
	for i in range(n):
		(n, s) = input().split()
		d = s.count('D') % 10
		u = s.count('U') % 10
		if d > u:
			d = d - u
			ans += str((a[i] + d) % 10) + ' '
		else:
			u = u - d
			ans += str((a[i] - u) % 10) + ' '
	print(ans)


def after_rotation(number, stri, b):
	for i in stri:
		if i == 'D':
			number += 1
		elif i == 'U':
			number -= 1
	pre_value = number % 10
	return pre_value
t = int(input())
while t > 0:
	n = int(input())
	numbers = list(map(int, input().split(' ')))
	answer_string = ''
	for i in range(n):
		(b, s) = input().split(' ')
		b = int(b)
		value = after_rotation(numbers[i], s, b)
		answer_string += str(value) + ' '
	print(answer_string)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		(a, b) = input().split()
		a = int(a)
		for j in range(a):
			if b[j] == 'U':
				l[i] = (l[i] - 1) % 10
			else:
				l[i] = (l[i] + 1) % 10
	print(' '.join(list(map(str, l))))


t = int(input())

def logic(x, curr):
	rem = x % 10
	curr += rem
	if curr > 9:
		ans = curr - 10
		return ans
	if curr < 0:
		ans = curr + 10
		return ans
	return curr
for test in range(t):
	reels = int(input())
	final = list(map(int, input().split()))
	breaked = ''
	for i in range(reels):
		curr = final[i]
		(step, code) = input().split()
		step = int(step)
		(d, u) = (code.count('D'), code.count('U'))
		x = d - u
		if x == 0:
			ans = curr
		else:
			ans = logic(x, curr)
		digit = str(curr + ans)
		breaked += str(ans) + ' '
	print(breaked)


t = int(input())
for i in range(t):
	n = int(input())
	init_num = list(map(int, input().split()))
	for j in range(len(init_num)):
		(cnt, move) = input().split()
		for k in move:
			if k == 'D':
				init_num[j] = (init_num[j] + 1) % 10
			else:
				init_num[j] = (init_num[j] - 1) % 10
	print(' '.join(list(map(str, init_num))))


t = int(input())
for i in range(t):
	n = int(input())
	arrabc = list(map(int, input().split()))
	thestr = []
	for q in range(n):
		(k, j) = map(str, input().split())
		u = j.count('U')
		d = j.count('D')
		ud = u - d
		if ud > 0:
			c = arrabc[q] - ud
			if c < 0:
				c += 10
			thestr.append(str(c))
		elif ud < 0:
			c = arrabc[q] + abs(ud)
			if c >= 10:
				c -= 10
			thestr.append(str(c))
		elif ud == 0:
			c = arrabc[q]
			thestr.append(str(c))
	print(' '.join(thestr))


def main():
	n = int(input())
	d = [int(p) for p in input().split()]
	for pos in range(n):
		(_, ops) = input().split()
		cnt = 0
		for op in ops:
			cnt += 1 if op == 'D' else -1
		cnt = (cnt + 10) % 10
		d[pos] = (d[pos] + cnt) % 10
	for j in d:
		print(j, end=' ')
	print()
t = int(input())
for i in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n):
		(b, s) = input().split()
		arr[i] = (arr[i] + int(b) - 2 * s.count('U')) % 10
	print(*arr)


for i in range(int(input())):
	s = int(input())
	k = list(map(int, input().split()))
	for j in range(s):
		a = input().split()
		x = len(a[1].replace('D', ''))
		y = len(a[1].replace('U', ''))
		if x > y:
			v = x - y
			if v > k[j]:
				k[j] = (k[j] - v % 10 + 10) % 10
			else:
				k[j] = k[j] - v % 10
		else:
			v = y - x
			k[j] = (k[j] + v) % 10
	for j in range(s):
		print(k[j], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		(b, s) = input().split()
		a[i] = (a[i] - s.count('U') + s.count('D')) % 10
	print(' '.join(map(str, a)))


for times in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	for i in range(n):
		val = input().split()[1]
		arr[i] = (arr[i] + val.count('D') - val.count('U')) % 10
	print(*arr)


def solution(tc):
	result = []
	for i in range(tc):
		w = int(input())
		o = [int(x) for x in input().split()]
		rr = []
		for j in range(w):
			l = input()
			d = l.count('D')
			u = l.count('U')
			t = d - u
			r = o[j] + t
			rr.append(str(r % 10))
		result.append(' '.join(rr))
	for m in result:
		print(m)
solution(int(input()))


T = int(input())
out = []
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	for j in range(N):
		(b, s) = input().split()
		D = s.count('D')
		U = s.count('U')
		A[j] += D - U
		A[j] %= 10
	out.append(A)
for o in out:
	print(*o)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(n):
		b = input().split()
		y = b[1].count('D')
		x = b[1].count('U')
		a[j] = a[j] + y - x
		if a[j] > 9:
			a[j] = a[j] % 10
		elif a[j] < 0:
			a[j] = (10 - -a[j] % 10) % 10
	print(*a)


import sys
ncases = int(input())

def solve():
	n = int(input())
	w = [int(x) for x in input().split(' ')]
	moves = [input().split(' ') for x in range(n)]
	data = [(w[x], moves[x][0], moves[x][1]) for x in range(n)]
	step = []
	for x in data:
		m = 0
		for y in x[2]:
			m += 1 if y == 'U' else -1
		step.append(str((x[0] - m) % 10))
	res.append(' '.join(step))
res = []
for x in range(ncases):
	solve()
[print(n) for n in res]


for _ in range(int(input())):
	n = int(input())
	state = list(map(int, input().split()))
	for i in range(n):
		commands = input().split()[1]
		reminder = (state[i] + commands.count('D') - commands.count('U')) % 10
		state[i] = reminder if reminder >= 0 else 10 - reminder
	print(*state)


x = int(input())
for jj in range(x):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	for i in range(n):
		(a, b) = input().split(' ')
		arr[i] = str((arr[i] + b.count('D') - b.count('U')) % 10)
	print(' '.join(arr))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	current = list(map(int, input().split()))
	for i in range(n):
		(b, s) = input().split()
		temp = s.count('D')
		change = abs(int(b) - temp - temp)
		if int(b) - temp > temp:
			if change > current[i]:
				current[i] = 10 - (change - current[i])
			else:
				current[i] = current[i] - change
		elif change + current[i] <= 9:
			current[i] = current[i] + change
		else:
			current[i] = change + current[i] - 10
	for i in current:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(len(arr)):
		el = arr[i]
		(numb, seq) = input().split()
		numb = int(numb)
		seq = list(seq)
		cnt_up = seq.count('U')
		cnt_down = len(seq) - cnt_up
		cnt = cnt_down - cnt_up
		el = (10 + el + cnt) % 10
		arr[i] = el
	print(*arr)


import math
from collections import Counter
import sys

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def solve(arr, ajj):
	for i in range(len(arr)):
		a = ajj[i].count('U')
		b = ajj[i].count('D')
		if a == b:
			continue
		if a < b:
			arr[i] = (arr[i] + (b - a)) % 10
		else:
			arr[i] = (arr[i] + 10 - (a - b)) % 10
	return arr
t = int(input())
for _ in range(t):
	n = get_ints()
	arr = get_ints()
	ajj = []
	for i in range(n[0]):
		s1 = get_string().split()
		b = s1[1]
		ajj.append(b)
	print(*solve(arr, ajj))


n = int(input())
for i in range(n):
	k = int(input())
	wheels = [int(i) for i in input().split()]
	for j in range(k):
		(step, move) = input().split()
		down = move.count('D')
		up = move.count('U')
		sub = down - up
		wheels[j] += sub
		if wheels[j] < 0:
			wheels[j] += 10
		elif wheels[j] > 9:
			wheels[j] -= 10
	print(*wheels)


for _ in range(int(input())):
	n = int(input())
	digs = [int(i) for i in input().split()]
	for i in range(n):
		(ops, moves) = input().split()
		ops = int(ops)
		ups = 0
		downs = 0
		for move in moves:
			if move == 'U':
				ups += 1
			else:
				downs += 1
		digs[i] -= ups
		digs[i] += downs
		digs[i] = digs[i] % 10
	print(' '.join([str(i) for i in digs]))


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	code = [0] * n
	for _ in range(n):
		(a, b) = map(str, input().split())
		u = b.count('U')
		d = b.count('D')
		plus = d - u
		a = l[_]
		a += plus
		while a > 9:
			a = a % 10
		while a < 0:
			a += 10
		l[_] = a
	print(*l)


for _ in range(int(input().strip())):
	n = int(input())
	w = list(map(int, input().split()))
	a = []
	for i in range(n):
		b = input().split()
		c = b[-1].count('D') - b[-1].count('U')
		a.append((w[i] + c + 10) % 10)
	print(*a)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	not_ans = []
	for j in range(len(c)):
		inp = str(input())
		inp2 = inp.split(' ')
		inp2.pop(0)
		samplestr = ''
		for x in inp2:
			samplestr += x
		for k in samplestr:
			if k == 'D':
				if c[j] == 9:
					c[j] = 0
				else:
					c[j] += 1
			elif k == 'U':
				if c[j] == 0:
					c[j] = 9
				else:
					c[j] -= 1
		not_ans.append(str(c[j]))
	ans = ''
	for l in not_ans:
		xy = l + ' '
		ans += xy
	print(ans[:-1])


for _ in range(int(input())):
	n = int(input())
	s = [int(x) for x in input().split()]
	moves = []
	change = []
	for x in range(n):
		moves.append(input().split()[1])
	for x in range(n):
		s[x] = s[x] + (moves[x].count('D') - moves[x].count('U'))
		if s[x] > 9:
			s[x] = s[x] - 9 - 1
		elif s[x] < 0:
			s[x] = 9 + s[x] + 1
	print(*s)


def rev(op, num):
	for c in op[::-1]:
		if c == 'U':
			if num == 0:
				num = 9
			else:
				num -= 1
		elif num == 9:
			num = 0
		else:
			num += 1
	return num

def solve(fw, ops, n):
	ans = []
	for i in range(n):
		cur = rev(ops[i], fw[i])
		ans.append(cur)
	return ' '.join(map(str, ans))

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		s = input().split()
		fw = list(map(int, s))
		ops = []
		for i in range(n):
			s = input().split()
			ops.append(s[1])
		res = solve(fw, ops, n)
		print(res)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = []
	for i in range(n):
		(nn, ss) = input().split()
		s = sum([1 if j == 'U' else -1 for j in ss])
		ans.append((a[i] - s) % 10)
	print(' '.join([str(j) for j in ans]))


def problem():
	lSize = int(input())
	finalCombination = list(map(int, input().split()))
	ans = []
	for ele in finalCombination:
		(n, steps) = input().split()
		n = int(n)
		steps = list(steps)
		for step in steps:
			if step == 'D':
				ele = ele + 1
				if ele == 10:
					ele = 0
			else:
				ele = ele - 1
				if ele == -1:
					ele = 9
		ans.append(ele)
	for ele in ans:
		print(ele, end=' ')
	print()
	return
t = int(input())
while t:
	problem()
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for (i, x) in enumerate(a):
		(_, s) = input().split()
		a[i] = (x + s.count('D') - s.count('U')) % 10
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = a
	for i in range(0, n):
		h = input().split()
		c = h[1]
		d = c.count('D') % 10
		u = c.count('U') % 10
		a1[0] += d - u
		a[i] = a1[0] % 10
		a1 = a1[1:]
	print(*a)


count = int(input())
for i in range(count):
	input()
	spisok = input().split()
	for c in range(len(spisok)):
		text = input().split()
		s = 0
		for o in text[1]:
			if o == 'U':
				s -= 1
			elif o == 'D':
				s += 1
		result = (int(spisok[c]) + s) % 10
		if result < 0:
			result = 10 + result
		spisok[c] = str(result)
	print(' '.join(spisok))


t = int(input())
answer = [None] * t
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(n):
		(_, b) = input().split()
		for r in reversed(b):
			if r == 'D':
				a[j] += 1
			else:
				a[j] -= 1
		a[j] %= 10
		if a[j] < 0:
			a[j] = 10 - a[j]
	answer[i] = a
for item in answer:
	print(*item)


import math
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	for i in range(n):
		(n1, s) = input().split()
		ucount = s.count('U')
		dcount = s.count('D')
		a = ucount - dcount
		if a <= 0:
			ans.append(abs(abs(a) + l[i]) % 10)
		else:
			val = l[i] - a
			if math.ceil(val / 10) <= 1:
				ans.append(val % 10)
			else:
				ans.append(10 - val)
	print(*ans)


for _ in ' ' * int(input()):
	n = int(input())
	ar = list(map(int, input().split()))
	for j in range(n):
		(a, b) = input().split()
		a = int(a)
		b = list(b)
		for i in b:
			if i == 'U':
				if ar[j] == 0:
					ar[j] = 9
				else:
					ar[j] -= 1
			elif ar[j] == 9:
				ar[j] = 0
			else:
				ar[j] += 1
	print(*ar)


for _ in range(int(input())):
	n = int(input())
	y = list(map(int, input().split()))
	results = []
	for i in range(n):
		(a, b) = input().split()
		e = b.count('D') - b.count('U')
		results.append((y[i] + e) % 10)
	print(*results)


def scroll(A, n):
	for i in range(int(n)):
		(k, M) = input().split()
		for x in M:
			if x == 'D':
				A[i] = (A[i] + 1) % 10
			elif x == 'U' and A[i] == 0:
				A[i] = 9
			else:
				A[i] -= 1
	print(*A)
for _ in range(int(input())):
	n = int(input())
	A = [int(x) for x in input().split()]
	scroll(A, n)


for i in range(int(input())):
	n = int(input())
	lst = [int(i) for i in input().split()]
	for i in range(len(lst)):
		x = input()
		(a, b) = x.split(' ')
		lstt1 = []
		lstt2 = []
		for j in b:
			if j == 'U':
				lstt1.append(j)
			elif j == 'D':
				lstt2.append(j)
		xor = lst[i] + len(lstt2)
		lst[i] = xor
		xrr = lst[i] - len(lstt1)
		lst[i] = xrr
	for i in lst:
		if i > 9:
			i -= 10
		elif i < 0:
			i += 10
		print(i, end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	a = map(int, input().split())
	l = list(a)
	for j in range(n):
		b = input()
		lb = list(b)
		fd = lb.count('D')
		fu = lb.count('U')
		d = fu - fd
		l[j] = l[j] - d
		if l[j] >= 10:
			l[j] = l[j] - 10
		if l[j] < 0:
			l[j] = l[j] + 10
	print(*l)


t = int(input())

def solve(s, e):
	earr = [char for char in e]
	for ele in earr:
		if ele == 'D':
			if s == 9:
				s = 0
			else:
				s = s + 1
		elif ele == 'U':
			if s == 0:
				s = 9
			else:
				s = s - 1
	return s
for i in range(t):
	no_wheel = int(input())
	final_seq = [int(i) for i in input().split(' ')]
	a = 0
	res_arr = []
	for seq in final_seq:
		arr = [i for i in input().split(' ')]
		res_arr.append(solve(seq, arr[1]))
	for i in res_arr:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	final = list(map(int, input().split()))
	for i in range(n):
		arr = input().split()[1]
		arr = list(arr)
		res = arr.count('D') - arr.count('U')
		final[i] += res
		final[i] %= 10
	for val in final:
		print(val, end=' ')
	print()


t = int(input())
while t:
	t -= 1
	n = int(input())
	wheels = list(map(int, input().split()))
	for i in range(n):
		[seqLength, seq] = input().split()
		for each in seq:
			if each == 'U':
				wheels[i] -= 1
				if wheels[i] < 0:
					wheels[i] = 9
			else:
				wheels[i] += 1
				if wheels[i] > 9:
					wheels[i] = 0
	for each in wheels:
		print(each, end=' ')
	print()


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	for j in range(n):
		move = list(map(str, input().split()))
		p = l[j]
		for m in move[1]:
			if m == 'U':
				if p == 0:
					p = 9
				else:
					p -= 1
			if m == 'D':
				if p == 9:
					p = 0
				else:
					p += 1
		l[j] = str(p)
	print(' '.join(l))


t = int(input())
for i in range(t):
	n_d = int(input())
	digit_lst = list(map(int, input().split()))
	new_lst = []
	for i in range(n_d):
		(moves, s) = input().split()
		moves = int(moves)
		for ele in s:
			if digit_lst[i] == 9 and ele == 'D':
				digit_lst[i] = 0
				continue
			if digit_lst[i] == 0 and ele == 'U':
				digit_lst[i] = 9
				continue
			if ele == 'U':
				digit_lst[i] -= 1
				continue
			if ele == 'D':
				digit_lst[i] += 1
				continue
	new_s = ''
	for ele in digit_lst:
		new_s = new_s + str(ele) + ' '
	print(new_s)


t = int(input())
a = 1
while t > 0:
	t = t - 1
	n = int(input())
	if a == 1:
		l1 = list(map(int, input().split()))
	for i in range(n):
		if a == 1:
			n = n - 1
		(m, s) = input().split()
		l1[i] = (l1[i] + s.count('D') - s.count('U')) % 10
	for j in l1:
		print(j, end=' ')
	print()


import sys
import heapq
input = sys.stdin.readline
from collections import Counter, defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def main():
	n = inp()
	wheels = inlt()
	for i in range(n):
		(count, turns) = input().strip().split(' ')
		for direction in turns:
			if direction == 'U':
				wheels[i] -= 1
				if wheels[i] == -1:
					wheels[i] = 9
			else:
				wheels[i] += 1
				if wheels[i] == 10:
					wheels[i] = 0
	print(' '.join([str(wheel) for wheel in wheels]))
t = inp()
for _ in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	for i in range(n):
		(r, d) = input().split()
		for j in d:
			a[i] += 1 if j == 'D' else -1
		a[i] %= 10
	print(*a)


for i in range(int(input())):
	turn = int(input())
	shown = list(map(int, input().split()))
	ans = str()
	for j in range(turn):
		l = input().split()
		shown[j] += l[1].count('D') - l[1].count('U')
		if shown[j] > 9:
			shown[j] = shown[j] % 10
		elif shown[j] == -10:
			shown[j] = 0
		elif shown[j] < 0:
			shown[j] = 10 - abs(shown[j]) % 10
		ans += str(shown[j]) + ' '
	print(ans.rstrip())


t = int(input())
for i in range(0, t):
	n = int(input())
	lst = list(map(int, input().split()))
	for j in range(0, n):
		(a, b) = input().split()
		a = int(a)
		b = list(b)
		u1 = b.count('U')
		u2 = b.count('D')
		uf = u1 - u2
		lst[j] = lst[j] - uf
		if lst[j] > 9:
			lst[j] = lst[j] - 10
		if lst[j] < 0:
			lst[j] = lst[j] + 10
	print(*lst)


t = int(input())
for i in range(t):
	n = int(input())
	num = list(map(int, input().split()))
	res = num
	for j in range(n):
		(pos, val) = [_ for _ in input().split()]
		res[j] = str((val.count('D') + val.count('U') * -1 + num[j]) % 10)
	print(' '.join(res))


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	l = []
	for i in range(n):
		s = input()
		k = int(s[0])
		l.append(s[2:])
	for i in range(n):
		i1 = l[i].count('U')
		i2 = l[i].count('D')
		c = abs(i1 - i2)
		if i1 > i2:
			d = arr[i] - c
			if d < 0:
				print(10 + d, end=' ')
			else:
				print(d, end=' ')
		elif i1 == i2:
			print(arr[i], end=' ')
		else:
			p = arr[i] + c
			if p >= 10:
				print(abs(10 - p), end=' ')
			else:
				print(p, end=' ')
	print()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(u, d) = (0, 0)
	hasil = 0
	res = [0] * n
	for i in range(n):
		(x, s) = input().split(' ')
		u = s.count('U')
		d = s.count('D')
		hasil = arr[i] - (u - d)
		if hasil > 9:
			hasil = hasil - 10
		elif hasil < 0:
			hasil = hasil + 10
		res[i] = hasil
	for i in range(n - 1):
		print(res[i], end=' ')
	print(res[-1])


n = int(input())
for c in range(n):
	nmexidas = int(input())
	cadeado = [int(x) for x in input().split()]
	for s in range(nmexidas):
		(a, b) = input().split()
		b = str(b)
		up = b.count('D')
		down = b.count('U')
		cadeado[s] = cadeado[s] + up - down
		if cadeado[s] > 9:
			cadeado[s] = cadeado[s] - 10
		elif cadeado[s] < 0:
			cadeado[s] = cadeado[s] + 10
	print(*cadeado)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	for i in range(n):
		(x, s) = input().split()
		for c in s:
			if c == 'U':
				l[i] = (l[i] - 1) % 10
			else:
				l[i] = (l[i] + 1) % 10
	print(*l)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		(a, s) = input().split()
		a = int(a)
		for e in s:
			if e == 'D':
				if l[i] == 9:
					l[i] = 0
				else:
					l[i] += 1
			elif l[i] == 0:
				l[i] = 9
			else:
				l[i] -= 1
	print(*l, sep=' ')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		(b, m) = input().split()
		d = m.count('D')
		u = m.count('U')
		a[i] = a[i] + d - u
		if a[i] >= 10 or a[i] < 0:
			a[i] %= 10
	print(' '.join(list(map(str, a))))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		(b, s) = input().split()
		for j in s:
			if j == 'D':
				a[i] += 1
			if j == 'U':
				a[i] -= 1
		if a[i] > 9:
			a[i] %= 10
		elif a[i] < 0:
			a[i] = (a[i] + 10) % 10
	print(*a)


def solve(l, q):
	res = []
	for i in range(len(l)):
		res.append((l[i] - q[i].count('U') + q[i].count('D')) % 10)
	return res

def main():
	t = int(input())
	l = []
	res = []
	for i in range(t):
		temp = []
		q = []
		n = int(input())
		temp = input()
		temp = temp.split()
		for i in range(n):
			temp[i] = int(temp[i])
		for j in range(n):
			line = input()
			line = line.split()
			q.append(line[1])
		res.append(solve(temp, q))
	for a in res:
		for k in a:
			print(k, end=' ')
		print()
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	for j in range(n):
		t = input().split()
		t = t[1]
		temp = l[j]
		for k in t:
			if k == 'U':
				if temp == 0:
					temp = 9
				else:
					temp -= 1
			elif temp == 9:
				temp = 0
			else:
				temp += 1
		l[j] = temp
	for k in l:
		print(k, end=' ')
	print()


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = []
	for i in range(a):
		(d, e) = input().split()
		c += [e]
	f = []
	for i in range(a):
		g = c[i].count('D')
		h = c[i].count('U')
		b[i] = (b[i] - h) % 10
		b[i] = (b[i] + g) % 10
	print(*b)


from collections import Counter
for _ in ' ' * int(input()):
	num = int(input())
	l = list(map(int, input().split()))
	for index in range(num):
		a = Counter(input()[2:])
		for i in a.items():
			if i[0] == 'D':
				l[index] = (l[index] + i[1]) % 10
			elif i[0] == 'U':
				l[index] = (l[index] - i[1] + 10) % 10
	print(' '.join(list(map(str, l))))


import math
from math import inf
from bisect import bisect_left, bisect_right
import itertools
import functools
import threading
from collections import deque
from queue import PriorityQueue
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		(_, actions) = input().split()
		counter = 0
		for action in actions:
			if action == 'U':
				counter -= 1
			elif action == 'D':
				counter += 1
		a[i] += counter
		a[i] %= 10
	print(*a)


from collections import Counter, defaultdict
from bisect import bisect_left
import math
n = int(input())
ans = []
for _ in range(n):
	t = int(input())
	temp = list(map(int, input().split()))
	for i in range(t):
		(_, c) = input().split()
		count = Counter(c)
		res = count['D'] - count['U']
		temp[i] = (temp[i] + res) % 10
	ans.append(temp)
for a in ans:
	print(*a)


def f(arr, n, steps):
	ans = []
	for i in range(n):
		temp = arr[i]
		st = steps[i]
		for j in st:
			if j == 'D':
				if temp == 9:
					temp = 0
				else:
					temp += 1
			elif j == 'U':
				if temp == 0:
					temp = 9
				else:
					temp -= 1
		ans.append(temp)
	return ans
for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	steps = []
	for i in range(n):
		a = [i for i in input().split()]
		steps.append(a[1])
	ans = f(arr, n, steps)
	for i in ans:
		print(i, end=' ')
	print()