Every character in the string “IITMANDI” is given a certain number of points. You are given a scrabble board with only one row. The input contains the positions of score modifiers such as: Double Letter, Triple Letter, Double Word and Triple Word. You need to find the best position to place the string “IITMANDI” such that your score is maximized. 
Double Letter - Doubles the number of points you get for the letter placed on the double letter.
Triple Letter - Triples the number of points you get for the letter placed on the triple letter.
Double Word - Doubles the number of points you get for the word. Applied after applying above modifiers.
Triple Word - Triples the number of points you get for the word. Applied after applying the above modifiers.
The word has to be read from left to right. You can’t place it in the reverse direction.

The letters have to be placed continuously on the board.

If there is no modifier or a double word or triple word modifier before a tile, it's score is added to the total score. The double word and triple modifiers are applied at the end.

-----Input Format-----
- First line containes a single integer $T$ - the number of test cases.
- First line of each test case contains a single integer $N$ - the size of the board.    
- Second line of each test case contains a string of size $N$ representing the board according to the following convention:

'.' - No modifier

'd' - Double letter

't' - Triple letter

'D' - Double word

'T' - Triple word     
- Third line of each test case contains 8 integers corresponding to the points associated with each letter of the string "IITMANDI". Note that the 3 'I's in IITMANDI cannot be interchanged freely. The score of the first 'I' will be equal to the first integer, score of the second 'I' will be equal to the second integer and the score of the last 'I' will be equal to the last integer.

-----Output Format-----
For each test case, output a single integer in a new line, the maximum possible score. 

-----Constraints-----
$ 1 \leq T \leq 1000 $
$ 8 \leq N \leq 100 $
$ 0 \leq $ Points for each character $ \leq 10^5 $ 

-----Sample Input-----
2

10

..d.t.D..d

10 11 12 9 8 10 11 15

22

dtDtTD..ddT.TtTdDT..TD

12297 5077 28888 17998 12125 27400 31219 21536

-----Sample Output-----
270

35629632

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
T = int(input())
for i in range(T):
	N = int(input())
	A = input()
	B = input()
	C = list(A)
	D = list(map(int, B.split()))
	E = []
	sum = 0
	b = 1
	for i in range(len(D)):
		sum = sum + D[i]
		a = sum
	for i in range(0, N - 7):
		for j in range(i, i + 8):
			if C[j] == '.':
				sum = sum
			elif C[j] == 'd':
				sum = sum + D[j - i]
			elif C[j] == 't':
				sum = sum + 2 * D[j - i]
			elif C[j] == 'D':
				b = b * 2
			elif C[j] == 'T':
				b = b * 3
		sum = b * sum
		E.append(sum)
		sum = a
		b = 1
	F = list(map(int, E))
	print(max(F))


t = int(input())
for i in range(t):
	num = int(input())
	arr = input()
	sc = list(map(int, input().split()))
	maxs = 0
	for z in range(num - 7):
		score = 0
		mul = 1
		for b in range(8):
			if arr[z + b] == 'd':
				score += sc[b] * 2
			elif arr[z + b] == 't':
				score += sc[b] * 3
			elif arr[z + b] == 'D':
				mul *= 2
				score += sc[b]
			elif arr[z + b] == 'T':
				mul *= 3
				score += sc[b]
			else:
				score += sc[b]
		score *= mul
		if score > maxs:
			maxs = score
	print(maxs)


t = int(input())
for _ in range(t):
	n = int(input())
	board = input()
	points = list(map(int, input().split()))
	maxx = 0
	for i in range(n - 7):
		dw = 0
		tw = 0
		score = 0
		for j in range(8):
			if board[i + j] == '.':
				score += points[j]
			elif board[i + j] == 'd':
				score += 2 * points[j]
			elif board[i + j] == 't':
				score += 3 * points[j]
			elif board[i + j] == 'D':
				score += points[j]
				dw += 1
			elif board[i + j] == 'T':
				score += points[j]
				tw += 1
		score = score * 2 ** dw
		score = score * 3 ** tw
		if score > maxx:
			maxx = score
	print(maxx)


t = int(input())
for z in range(t):
	n = int(input())
	board = input()
	b = list(board)
	maxscore = 0
	scores = list(map(int, input().split()))
	for i in range(0, n - 7):
		multiplier = 1
		score = 0
		for j in range(i, i + 8):
			if board[j] == '.':
				score += scores[j - i]
			elif board[j] == 'd':
				score += scores[j - i] * 2
			elif board[j] == 't':
				score += scores[j - i] * 3
			elif board[j] == 'T':
				score += scores[j - i]
				multiplier *= 3
			else:
				score += scores[j - i]
				multiplier *= 2
		maxscore = max(maxscore, multiplier * score)
	print(maxscore)


T = int(input())
for t in range(T):
	N = int(input())
	m = input()
	l = [int(x) for x in input().split()]
	maxscore = 0.0
	for i in range(len(m) - len(l) + 1):
		score = 0.0
		mul = 1.0
		for j in range(len(l)):
			if m[i + j] == '.':
				score += l[j]
			elif m[i + j] == 'd':
				score += l[j] * 2
			elif m[i + j] == 't':
				score += l[j] * 3
			elif m[i + j] == 'D':
				score += l[j]
				mul = mul * 2
			elif m[i + j] == 'T':
				score += l[j]
				mul = mul * 3
		score = score * mul
		maxscore = max(score, maxscore)
	print(int(maxscore))


t = int(input())
for _ in range(t):
	n = int(input())
	board = input()
	weight = list(map(int, input().split()))
	maxscore = -1
	for i in range(n - 8 + 1):
		squares = board[i:]
		score = 0
		mul = []
		for j in range(8):
			if squares[j] == 'd':
				score += 2 * weight[j]
			elif squares[j] == 't':
				score += 3 * weight[j]
			elif squares[j] == 'D':
				score += weight[j]
				mul.append(squares[j])
			elif squares[j] == 'T':
				score += weight[j]
				mul.insert(0, squares[j])
			elif squares[j] == '.':
				score += weight[j]
		while mul:
			x = mul.pop()
			if x == 'D':
				score *= 2
			elif x == 'T':
				score *= 3
		maxscore = max(maxscore, score)
	print(maxscore)


T = int(input())
for i in range(T):
	N = int(input())
	s = input()
	m1 = input()
	m = m1.split()
	for ii in range(8):
		m[ii] = int(m[ii])
	k = 0
	l = 0
	for j in range(N - 7):
		n = []
		for ij in range(len(m)):
			n.append(m[ij])
		for jj in range(8):
			if s[j + jj] == 'd':
				n[jj] = 2 * n[jj]
			elif s[j + jj] == 't':
				n[jj] = 3 * n[jj]
		l = sum(n)
		for jj in range(8):
			if s[j + jj] == 'D':
				l = l * 2
			elif s[j + jj] == 'T':
				l = l * 3
		k = max(k, l)
	print(k)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	p = []
	b = input().split()
	for j in range(8):
		b[j] = int(b[j])
	for k in range(n - 7):
		s = 0
		for h in range(8):
			if a[k + h] == 'd':
				s = s + 2 * b[h]
			elif a[k + h] == 't':
				s = s + 3 * b[h]
			else:
				s = s + b[h]
		D = a[k:k + 8].count('D')
		T = a[k:k + 8].count('T')
		s = s * pow(2, D)
		s = s * pow(3, T)
		p.append(s)
	print(max(p))


t = int(input())
for i in range(0, t):
	n = int(input())
	s = str(input())
	l = list(map(int, input().split()))
	sum1 = sum(l)
	finalSum = 0
	for i in range(n - 7):
		sum2 = 0
		dw = 1
		tw = 1
		for k in range(i, i + 8):
			if s[k] == 'd':
				sum2 = sum2 + 2 * l[k - i]
			elif s[k] == 't':
				sum2 = sum2 + 3 * l[k - i]
			elif s[k] == 'D':
				dw *= 2
				sum2 = sum2 + l[k - i]
			elif s[k] == 'T':
				tw *= 3
				sum2 = sum2 + l[k - i]
			else:
				sum2 = sum2 + l[k - i]
		finalSum = max(finalSum, sum2 * dw * tw)
	print(finalSum)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	ar = input().split()
	for j in range(8):
		ar[j] = int(ar[j])
	max = 0
	for j in range(n - 7):
		p = 0
		a = 0
		b = 0
		for k in range(8):
			if s[j + k] == 'd':
				p = p + ar[k] * 2
			elif s[j + k] == 't':
				p = p + ar[k] * 3
			elif s[j + k] == 'D':
				a = a + 1
				p = p + ar[k]
			elif s[j + k] == 'T':
				b = b + 1
				p = p + ar[k]
			else:
				p = p + ar[k]
		p = p * 2 ** a * 3 ** b
		if p > max:
			max = p
	print(max)


t = int(input())
while t > 0:
	n = int(input())
	s = str(input())
	A = list(map(int, input().split()))
	maxi = 0
	for i in range(0, n - 7):
		summ = 0
		new = ''
		k = 0
		for j in range(i, i + 8):
			if s[j] == 'D' or s[j] == 'T':
				new = new + s[j]
			if s[j] == 'd':
				summ += 2 * A[k]
			elif s[j] == 't':
				summ += 3 * A[k]
			else:
				summ += A[k]
			k += 1
		for x in new:
			if x == 'D':
				summ = 2 * summ
			else:
				summ = 3 * summ
		if summ > maxi:
			maxi = summ
	print(maxi)
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	b = input()
	s = list(map(int, input().split()))
	ans = 0
	for i in range(n - 7):
		score = 0
		d = 0
		t = 0
		for j in range(8):
			if b[i + j] == '.':
				score += s[j]
			elif b[i + j] == 'd':
				score += 2 * s[j]
			elif b[i + j] == 't':
				score += 3 * s[j]
			elif b[i + j] == 'D':
				score += s[j]
				d += 1
			else:
				score += s[j]
				t += 1
		score = score * pow(2, d) * pow(3, t)
		ans = max(ans, score)
	print(ans)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	l = input()
	value = input().split(' ')
	for i in range(8):
		value[i] = int(value[i])
	m = [0] * (n - 7)
	for i in range(n - 7):
		D = 0
		T = 0
		for j in range(8):
			if l[j + i] == '.':
				m[i] += value[j]
			if l[j + i] == 'd':
				m[i] += 2 * value[j]
			if l[j + i] == 't':
				m[i] += 3 * value[j]
			if l[j + i] == 'D':
				m[i] += value[j]
				D += 1
			if l[j + i] == 'T':
				m[i] += value[j]
				T += 1
		m[i] = m[i] * 2 ** D * 3 ** T
	print(max(m))


def calc(W, board):
	d = t = score = 0
	for i in range(8):
		if board[i] == 'd':
			score += 2 * W[i]
		elif board[i] == 't':
			score += 3 * W[i]
		elif board[i] == 'D':
			d += 1
			score += W[i]
		elif board[i] == 'T':
			t += 1
			score += W[i]
		else:
			score += W[i]
	score *= 2 ** d * 3 ** t
	return score
output = ''
for test in range(int(input())):
	N = int(input())
	board = input()
	W = [int(x) for x in input().split()]
	m = -1
	for i in range(N - 7):
		m = max(m, calc(W, board[i:i + 8]))
	output += str(m) + '\n'
print(output.rstrip())


from collections import defaultdict as dd
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	score = list(map(int, input().split()))
	ans = []
	k = 8
	for i in range(n - k + 1):
		trim = s[i:i + k]
		cur = 0
		D = 0
		T = 0
		for j in range(k):
			if trim[j] == '.':
				cur += score[j]
			elif trim[j] == 'd':
				cur += score[j] * 2
			elif trim[j] == 't':
				cur += score[j] * 3
			elif trim[j] == 'D':
				cur += score[j]
				D += 1
			else:
				cur += score[j]
				T += 1
		if D:
			cur *= pow(2, D)
		if T:
			cur *= pow(3, T)
		ans.append(cur)
	if len(ans) == 0:
		ans = 0
	else:
		ans = max(ans)
	print(ans)


import math

def main():
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		s = input()
		point = input().split()
		point = [int(i) for i in point]
		max = 0
		for i in range(n - 7):
			score = 0
			d_freq = 0
			t_freq = 0
			for j in range(8):
				if s[i + j] == 'd':
					score += 2 * point[j]
				elif s[i + j] == 't':
					score += 3 * point[j]
				elif s[i + j] == 'D':
					d_freq += 1
					score += point[j]
				elif s[i + j] == 'T':
					t_freq += 1
					score += point[j]
				else:
					score += point[j]
			score *= math.pow(2, d_freq)
			score *= math.pow(3, t_freq)
			if score > max:
				max = score
		print(int(max))
main()


T = int(input())
for j in range(T):
	n = int(input())
	ar = input()
	ar_v = list(map(int, input().split()))
	mx = 0
	for i in range(n - 7):
		sm = 0
		m = 1
		for k in range(8):
			if ar[i + k] == '.':
				sm += ar_v[k]
			elif ar[i + k] == 'd':
				sm = sm + 2 * ar_v[k]
			elif ar[i + k] == 't':
				sm = sm + 3 * ar_v[k]
			elif ar[i + k] == 'D':
				sm = sm + ar_v[k]
				m *= 2
			elif ar[i + k] == 'T':
				sm = sm + ar_v[k]
				m *= 3
		sm = sm * m
		if mx < sm:
			mx = sm
	print(mx)


t = int(input())
for i in range(t):
	n = int(input())
	str = input()
	iit = list(map(int, input().split()))
	max = 0
	for j in range(n - 7):
		sum = 0
		d = 0
		t = 0
		for k in range(8):
			sum += iit[k]
			if str[j + k] == 'D':
				d += 1
			elif str[j + k] == 'T':
				t += 1
			elif str[j + k] == 'd':
				sum += iit[k]
			elif str[j + k] == 't':
				sum += 2 * iit[k]
		sum = sum * 2 ** d
		sum = sum * 3 ** t
		if sum >= max:
			max = sum
	print(max)


T = int(input())

def fnc(s, m):
	sum = 0
	lst = ['.', 'D', 'T']
	for i in range(8):
		if s[i] in lst:
			sum = sum + m[i]
		else:
			if s[i] == 'd':
				sum = sum + 2 * m[i]
			if s[i] == 't':
				sum = sum + 3 * m[i]
	v1 = s.count('D')
	v2 = s.count('T')
	sum = sum * 2 ** v1
	sum = sum * 3 ** v2
	return sum
for i in range(T):
	N = int(input())
	s = input()
	A = []
	ans = []
	m = input().split()
	for i in range(8):
		m[i] = int(m[i])
	for j in range(N - 7):
		A.append(s[j:j + 8])
	for x in A:
		ans.append(fnc(x, m))
	print(max(ans))


try:
	maxpoint = []

	def maxpoints(n, string, sc):
		i = 0
		points = 0
		increaser = 1
		while i < 8:
			if string[i] == '.':
				points += sc[i]
				i += 1
			elif string[i] == 'd':
				points += sc[i] * 2
				i += 1
			elif string[i] == 't':
				points += sc[i] * 3
				i += 1
			elif string[i] == 'D':
				increaser = increaser * 2
				points += sc[i]
				i += 1
			elif string[i] == 'T':
				increaser = increaser * 3
				points += sc[i]
				i += 1
		points = points * increaser
		maxpoint.append(points)
		return points

	def values(n, string, sc):
		for i in range(n - 7):
			maxpoints(n, string[i:], sc)
		return max(maxpoint)
	t = int(input())
	for i in range(t):
		n = int(input())
		string = str(input())
		if n >= 8 and n == len(string):
			sc = list(map(int, input().split()))
			print(values(n, string, sc))
			maxpoint = []
		else:
			print('String is not equals to the length given')
			break
except EOFError:
	pass


for e in range(int(input())):
	n = int(input())
	s = input()
	l = list(map(int, input().split()))
	s = list(s)
	m = 0
	for i in range(n - 7):
		total = 0
		k = 0
		DT = 1
		for j in range(i, i + 8):
			if s[j] == '.':
				total += l[k]
			elif s[j] == 'd':
				total += 2 * l[k]
			elif s[j] == 't':
				total += 3 * l[k]
			elif s[j] == 'D':
				DT = DT * 2
				total += l[k]
			elif s[j] == 'T':
				DT = DT * 3
				total += l[k]
			k += 1
		total = total * DT
		m = max(total, m)
	print(m)


for _ in range(int(input())):
	n = int(input())
	s = input()
	v = list(map(int, input().split()))
	ma = -1
	for x in range(n - 8 + 1):
		m = 1
		l = s[x:x + 8]
		r = v.copy()
		for i in range(8):
			if l[i] == 'd':
				r[i] *= 2
			if l[i] == 't':
				r[i] *= 3
			if l[i] == 'D':
				m *= 2
			if l[i] == 'T':
				m *= 3
		ma = max(ma, sum(r) * m)
	print(ma)


maxpoints = []

def maxvaluez(num, arr, sc):
	i = 0
	points = 0
	mult = 1
	while i < 8:
		if arr[i] == '.':
			points += sc[i]
			i += 1
		elif arr[i] == 'd':
			points += 2 * sc[i]
			i += 1
		elif arr[i] == 't':
			points += 3 * sc[i]
			i += 1
		elif arr[i] == 'D':
			mult = mult * 2
			points += sc[i]
			i += 1
		elif arr[i] == 'T':
			mult = mult * 3
			points += sc[i]
			i += 1
		else:
			i += 1
	points = points * mult
	maxpoints.append(points)
	return points

def maxpicker(num, arr, sc):
	for i in range(num - 7):
		maxvaluez(num, arr[i:], sc)
	return max(maxpoints)
try:
	t = int(input())
	for i in range(t):
		num = int(input())
		arr = input()
		sc = list(map(int, input().split()))
		print(maxpicker(num, arr, sc))
		maxpoints = []
except:
	pass


t = input()
t = int(t)
while t > 0:
	t = t - 1
	N = input()
	N = int(N)
	s = input()
	w = input().split(' ')
	for i in range(0, 8):
		w[i] = int(w[i])
	o = [0] * (N - 7)
	for i in range(0, N - 7):
		D = 0
		T = 0
		for j in range(0, 8):
			if s[j + i] == '.':
				o[i] += w[j]
			elif s[j + i] == 'd':
				o[i] += 2 * w[j]
			elif s[j + i] == 't':
				o[i] += 3 * w[j]
			elif s[j + i] == 'D':
				o[i] += w[j]
				D += 1
			else:
				o[i] += w[j]
				T += 1
		D = 2 ** D
		T = 3 ** T
		o[i] = o[i] * D * T
	maxx = max(o)
	print(maxx)


def fun(l, m):
	s = 0
	d = 0
	z = 0
	for i in range(len(m)):
		if m[i] == '.':
			s += l[i]
		elif m[i] == 'd':
			s += l[i] * 2
		elif m[i] == 't':
			s += l[i] * 3
		elif m[i] == 'D':
			s += l[i]
			d += 1
		elif m[i] == 'T':
			s += l[i]
			z += 1
	s = s * 2 ** d
	s = s * 3 ** z
	return s
t = int(input())
for kmj in range(t):
	n = int(input())
	sc = list(input())
	l = list(map(int, input().split()))
	max = fun(l, sc[:8])
	for i in range(len(sc) - 7):
		x = sc[i:i + 8]
		z = fun(l, x)
		if z > max:
			max = z
	print(max)


try:
	T = int(input())
	for i in range(T):
		n = int(input())
		s = input()
		arr = [int(i) for i in input().strip().split(' ')]
		res = 1
		result = 0
		for j in range(n - 7):
			res = 1
			res1 = 0
			s1 = s[j:j + 8]
			for i in range(8):
				if s1[i] == 'D':
					res = res * 2
					res1 += arr[i]
				elif s1[i] == 'T':
					res = res * 3
					res1 = res1 + arr[i]
				elif s1[i] == 'd':
					res1 = res1 + arr[i] * 2
				elif s1[i] == 't':
					res1 += arr[i] * 3
				else:
					res1 += arr[i]
			res = res * res1
			result = max(res, result)
		print(result)
except EOFError:
	pass


def f(a, x):
	r = sum(a)
	p = 1
	for i in range(len(x)):
		if x[i] == 't':
			r += 2 * a[i]
		elif x[i] == 'd':
			r += a[i]
		elif x[i] == 'D':
			p *= 2
		elif x[i] == 'T':
			p *= 3
	return p * r
for i in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	a = [int(x) for x in input().split()]
	t = len(s) - len(a) + 1
	for i in range(t):
		l = f(a, s[i:len(s) + i - t + 1])
		if l > m:
			m = l
	print(m)


for i in range(int(input())):
	n = int(input())
	a = input()
	A = list(a)
	S = list(map(int, input().split()))
	mas = 0
	for i in range(n - 7):
		s = 0
		d = 0
		t = 0
		for j in range(i, i + 8):
			if A[j] == 'd':
				s += S[j - i] * 2
			elif A[j] == 't':
				s += S[j - i] * 3
			else:
				s += S[j - i]
			if A[j] == 'D':
				d += 1
			if A[j] == 'T':
				t += 1
		ts = s
		if d > 0:
			ts = ts * 2 ** d
		if t > 0:
			ts = ts * 3 ** t
		if mas < ts:
			mas = ts
	print(mas)


t = int(input())
d = {'.': 1, 'd': 2, 't': 3}
for _ in range(t):
	n = int(input())
	st = input()
	l = list(map(int, input().split()))
	maxval = 0
	for i in range(n - 7):
		val = 0
		(Tu, Du) = (0, 0)
		for j in range(8):
			if st[i + j] == 'T':
				Tu += 1
				val += l[j]
			elif st[i + j] == 'D':
				Du += 1
				val += l[j]
			else:
				val += l[j] * d[st[i + j]]
		val *= pow(3, Tu) * pow(2, Du)
		val = int(val)
		maxval = max(maxval, val)
	print(maxval)


t = int(input())
for x in range(t):
	n = int(input())
	st = input()
	maximum = 0
	count = 0
	ls = list(map(int, input().strip().split()))
	for i in range(n - 7):
		s = st[i:i + 8]
		dd = 0
		tt = 0
		som = 0
		for j in range(8):
			if s[j] == '.':
				som = som + ls[j]
			elif s[j] == 'd':
				som = som + 2 * ls[j]
			elif s[j] == 't':
				som = som + 3 * ls[j]
			elif s[j] == 'D':
				dd = dd + 1
				som = som + ls[j]
			elif s[j] == 'T':
				tt = tt + 1
				som = som + ls[j]
		for y in range(dd):
			som = som * 2
		for z in range(tt):
			som = som * 3
		if som > maximum:
			maximum = som
	print(maximum)


def f(l, p):
	a = 0
	for i in range(8):
		if l[i] == 'd':
			a += p[i] * 2
		elif l[i] == 't':
			a += p[i] * 3
		else:
			a += p[i]
	a = a * 2 ** l.count('D') * 3 ** l.count('T')
	return a
t = int(input())
for i in range(t):
	n = int(input())
	l = list(input())
	p = list(map(int, input().split()))
	q = []
	for i in range(len(l) - 7):
		q.append(f(l[i:i + 8], p))
	print(max(q))


t = int(input())
maxi = []
for _ in range(t):
	n = int(input())
	board = input()
	arr = input()
	points = list(map(int, arr.split(' ')))
	values = []
	for i in range(n - 7):
		dw = 1
		tw = 1
		total = 0
		for j in range(i, i + 8):
			if board[j] == 'd':
				total += 2 * points[j - i]
			elif board[j] == 't':
				total += 3 * points[j - i]
			elif board[j] == 'D':
				dw *= 2
				total += points[j - i]
			elif board[j] == 'T':
				tw *= 3
				total += points[j - i]
			elif board[j] == '.':
				total += points[j - i]
		total *= dw
		total *= tw
		values.append(total)
	maxi.append(max(values))
for element in maxi:
	print(element)


for _ in range(int(input())):
	N = int(input())
	S = input()
	V = list(map(int, input().split()))
	vals = []
	for start in range(N - 7):
		double_word = 0
		triple_word = 0
		score = 0
		for (i, x) in enumerate(S[start:start + 8]):
			if x == '.':
				score += V[i]
			elif x == 'd':
				score += V[i] * 2
			elif x == 't':
				score += V[i] * 3
			elif x == 'D':
				score += V[i]
				double_word += 1
			else:
				score += V[i]
				triple_word += 1
		score = score * 2 ** double_word * 3 ** triple_word
		vals.append(score)
	print(max(vals))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	num = [int(j) for j in input().split()]
	ma = 0
	for j in range(n - 7):
		s2 = s[j:j + 8]
		Dcount = 0
		Tcount = 0
		a = num[:]
		for k in range(8):
			if s2[k] == '.':
				continue
			elif s2[k] == 'd':
				a[k] *= 2
			elif s2[k] == 't':
				a[k] *= 3
			elif s2[k] == 'D':
				Dcount += 1
			else:
				Tcount += 1
		su = sum(a)
		su *= 2 ** Dcount
		su *= 3 ** Tcount
		if su > ma:
			ma = su
	print(ma)


for i in range(int(input())):
	a = int(input())
	b = input()
	c = list(map(int, input().split()))
	m = sum(c)
	f = []
	for j in range(a - 7):
		l = m
		z = 1
		y = 1
		for k in range(j, j + 8):
			if b[k] == '.':
				continue
			elif b[k] == 'd':
				l += c[k - j]
			elif b[k] == 't':
				l += c[k - j] * 2
			elif b[k] == 'D':
				y = y * 2
			else:
				z = z * 3
		f.append(l * y * z)
	print(max(f))


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	N = [int(i) for i in input().split()]
	dic = {'.': 1, 'd': 2, 't': 3, 'D': 1, 'T': 1}
	maxx = 0
	for i in range(n - 8 + 1):
		st = s[i:i + 8]
		total = 0
		D = 0
		T = 0
		for j in range(8):
			if st[j] in ['.', 'd', 't']:
				total = total + dic[st[j]] * N[j]
			elif st[j] == 'D':
				total = total + dic[st[j]] * N[j]
				D = D + 1
			else:
				total = total + dic[st[j]] * N[j]
				T = T + 1
		total = total * 2 ** D
		total = total * 3 ** T
		if maxx < total:
			maxx = total
	print(maxx)


def maximumScoreingPosition(iitmandiint, board, N):
	array = []
	for k in range(N - 7):
		sums = 0
		wordMultipleir = 1
		for i in range(8):
			if board[k + i] == '.':
				sums += iitmandiint[i]
			elif board[k + i] == 'd':
				sums += iitmandiint[i] * 2
			elif board[k + i] == 't':
				sums += iitmandiint[i] * 3
			elif board[k + i] == 'D':
				sums += iitmandiint[i]
				wordMultipleir *= 2
			elif board[k + i] == 'T':
				sums += iitmandiint[i]
				wordMultipleir *= 3
		array.append(sums * wordMultipleir)
	return max(array)
T = int(input())
array = []
for i in range(T):
	N = int(input())
	board = input()
	iitmandiint = [int(i) for i in input().split()]
	array.append(maximumScoreingPosition(iitmandiint, board, N))
for i in array:
	print(i)


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

def string_list():
	return list(map(str, input().split()))

def hetro_list():
	return list(input().split())
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	points = integer_list()
	ans = 0
	for i in range(n - 7):
		string = s[i:i + 8]
		score = 0
		count_D = 0
		count_T = 0
		for j in range(8):
			if string[j] == '.':
				score += points[j]
			elif string[j] == 'd':
				score += points[j] * 2
			elif string[j] == 't':
				score += points[j] * 3
			elif string[j] == 'D':
				score += points[j]
				count_D += 1
			else:
				score += points[j]
				count_T += 1
		if count_D:
			score *= 2 ** count_D
		if count_T:
			score *= 3 ** count_T
		ans = max(ans, score)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	str = input()
	ar = input().split()
	br = []
	for j in ar:
		br.append(int(j))
	score = 0
	for j in range(n - 7):
		bf = 1
		sum = 0
		for k in range(8):
			f = 1
			if str[j + k] == 'd':
				f = 2
			elif str[j + k] == 't':
				f = 3
			elif str[j + k] == 'D':
				bf = bf * 2
			elif str[j + k] == 'T':
				bf = bf * 3
			sum = sum + f * br[k]
		sum = sum * bf
		if sum > score:
			score = sum
	print(score)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	scr = map(int, input().split())
	scr = list(scr)
	score = 0
	for j in range(n - 8 + 1):
		scr1 = 0
		for k in range(j, j + 8):
			if s[k] == 'd':
				scr1 += 2 * scr[k - j]
			elif s[k] == 't':
				scr1 += 3 * scr[k - j]
			else:
				scr1 += scr[k - j]
		for k in range(j, j + 8):
			if s[k] == 'D':
				scr1 = scr1 * 2
			elif s[k] == 'T':
				scr1 = scr1 * 3
		if scr1 > score:
			score = scr1
	print(score)


for _ in range(int(input())):
	n = int(input())
	B = input()
	L = [int(i) for i in input().split()]
	max_score = 0
	for i in range(n - 7):
		L1 = []
		B1 = []
		for j in range(8):
			c = B[i + j]
			if c == '.':
				L1.append(L[j])
			elif c == 'd':
				L1.append(2 * L[j])
			elif c == 't':
				L1.append(3 * L[j])
			else:
				L1.append(L[j])
				B1.append(c)
		score = sum(L1)
		for c in B1:
			if c == 'D':
				score *= 2
			else:
				score *= 3
		if score > max_score:
			max_score = score
	print(max_score)


test = int(input())
for i in range(test):
	c = 0
	n = int(input())
	s = input()
	a = list(map(int, input().split()))
	for j in range(len(s) - 7):
		tc = 0
		cd = 0
		ct = 0
		temp = []
		temp = s[j:j + 8]
		for k in range(len(temp)):
			if temp[k] == 'd':
				tc += a[k] * 2
			elif temp[k] == 't':
				tc += a[k] * 3
			elif temp[k] == 'D':
				cd += 1
				tc += a[k]
			elif temp[k] == 'T':
				ct += 1
				tc += a[k]
			else:
				tc += a[k]
		tc = tc * 2 ** cd
		tc = tc * 3 ** ct
		if tc > c:
			c = tc
	print(c)


for _ in range(int(input())):
	sum = 0
	count = 0
	max = 0
	n = int(input())
	st = input()
	li = [int(i) for i in input().split(' ')]
	flag = True
	for i in range(n - 7):
		k = 0
		count = 1
		sum = 0
		for j in range(i, i + 8):
			if st[j] == 'd':
				sum += li[k] * 2
			elif st[j] == 't':
				sum += li[k] * 3
			elif st[j] == 'D':
				sum += li[k]
				count *= 2
			elif st[j] == 'T':
				sum += li[k]
				count *= 3
			else:
				sum += li[k]
			k += 1
		if flag == True:
			max = sum * count
			flag = False
		elif sum * count > max:
			max = sum * count
	print(max)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = list(map(int, input().split()))
	maxpoints = 0
	word = sum(b)
	for i in range(n - 7):
		m = 1
		points = word
		for j in range(8):
			if a[i + j] == 'd':
				points += b[j]
			elif a[i + j] == 't':
				points += b[j] * 2
			elif a[i + j] == 'D':
				m *= 2
			elif a[i + j] == 'T':
				m *= 3
		points *= m
		if points > maxpoints:
			maxpoints = points
	print(maxpoints)


T = int(input())
max_score = []
for _ in range(T):
	N = int(input())
	string = input()
	score = input().split()
	score = list(map(int, score))
	noi = N - 8 + 1
	maximum = 0
	for i in range(noi):
		dd = 0
		ttt = 0
		scrs = string[i:i + 8]
		num_scrs = []
		for scr in scrs:
			if scr == '.':
				num_scrs.append(1)
			elif scr == 'd':
				num_scrs.append(2)
			elif scr == 't':
				num_scrs.append(3)
			elif scr == 'D':
				num_scrs.append(1)
				dd += 1
			elif scr == 'T':
				num_scrs.append(1)
				ttt += 1
		s = 0
		for i in range(8):
			s += num_scrs[i] * score[i]
		s *= 2 ** dd
		s *= 3 ** ttt
		if s > maximum:
			maximum = s
	max_score.append(maximum)
for s in max_score:
	print(s)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = list(map(int, input().strip().split()))
	e = []
	for j in range(n - 7):
		f = b.copy()
		g = 0
		c = 0
		d = 0
		for k in range(8):
			if a[j + k] == 'd':
				f[k] = f[k] * 2
			elif a[j + k] == 't':
				f[k] = f[k] * 3
			elif a[j + k] == 'D':
				c = c + 1
			elif a[j + k] == 'T':
				d = d + 1
		for l in range(len(f)):
			g = g + f[l]
		e.append(g * 2 ** c * 3 ** d)
	print(max(e))


pos_values = {'d': 2, 't': 3, '.': 1, 'D': 1, 'T': 1}
T = int(input())
for i in range(T):
	N = int(input())
	positions = input()
	values = input()
	list_of_values = []
	temp = 0
	prev = 0
	i = 0
	while i < len(values):
		if values[i] == ' ':
			temp = int(values[prev:i])
			list_of_values.append(temp)
			prev = i + 1
			i += 1
		else:
			i += 1
	temp = int(values[prev:i])
	list_of_values.append(temp)
	set_of_values = []
	for j in range(N - 7):
		temp = 0
		for k in range(8):
			temp += list_of_values[k] * pos_values.get(positions[j + k])
		for k in range(8):
			if positions[j + k] == 'D':
				temp *= 2
			elif positions[j + k] == 'T':
				temp *= 3
		set_of_values.append(temp)
	max_num = 0
	for l in range(len(set_of_values)):
		max_num = max(max_num, set_of_values[l])
	print(max_num)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	l = list(map(int, input().split()))
	c = 0
	for i in range(n - 8 + 1):
		x = 0
		d = 0
		t = 0
		m = 0
		for j in range(i, i + 8):
			if s[j] == '.':
				x += l[m]
			elif s[j] == 'd':
				x += 2 * l[m]
			elif s[j] == 't':
				x += 3 * l[m]
			elif s[j] == 'D':
				x += l[m]
				d += 1
			else:
				x += l[m]
				t += 1
			m += 1
		x = x * 2 ** d
		x = x * 3 ** t
		c = max(c, x)
	print(c)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input()
	l = list(a)
	res = []
	d = input().split()
	for i in range(n - 7):
		lis = l[i:i + 8]
		s = 0
		c_d = 0
		c_t = 0
		for j in range(8):
			if lis[j] == '.':
				s += int(d[j]) * 1
			elif lis[j] == 'd':
				s += int(d[j]) * 2
			elif lis[j] == 't':
				s += int(d[j]) * 3
			elif lis[j] == 'D':
				s += int(d[j]) * 1
				c_d += 1
			elif lis[j] == 'T':
				s += int(d[j]) * 1
				c_t += 1
		n_d = 2 ** c_d
		n_t = 3 ** c_t
		if n_t != 0:
			s = s * n_t
		if n_d != 0:
			s = s * n_d
		res.append(s)
	mx = max(res)
	print(mx)


T = int(input())
while T:
	N = int(input())
	s = str(input())
	l = list(map(int, input().split()))
	re = []
	for i in range(N - 7):
		sum = 0
		d1 = 1
		t1 = 1
		for j in range(len(l)):
			if s[i + j] == '.':
				sum = sum + l[j]
			elif s[i + j] == 'd':
				sum = sum + l[j] * 2
			elif s[i + j] == 't':
				sum = sum + l[j] * 3
			elif s[i + j] == 'D':
				sum = sum + l[j]
				d1 = d1 * 2
			elif s[i + j] == 'T':
				sum = sum + l[j]
				t1 = t1 * 3
		if d1 > 1 and t1 > 1:
			sum = sum * d1 * t1
		elif d1 > 1:
			sum = sum * d1
		else:
			sum = sum * t1
		re.append(sum)
	print(max(re))
	T = T - 1


t = int(input())
for _ in range(t):
	n = int(input())
	board = list(input())
	points = [int(x) for x in input().split()]
	ans = 0
	for i in range(0, n - 7):
		double = 0
		triple = 0
		point = 0
		bo = board[i:i + 8]
		for b in range(len(bo)):
			if bo[b] == 'd':
				point += points[b] * 2
			elif bo[b] == 't':
				point += points[b] * 3
			else:
				point += points[b]
			if bo[b] == 'D':
				double += 1
			elif bo[b] == 'T':
				triple += 1
		point = point * pow(2, double)
		point = point * pow(3, triple)
		ans = max(ans, point)
	print(ans)


t = int(input())
ans = list()
while t > 0:
	n = int(input())
	lay = input()
	pos = n - 7
	scr = 0
	letters = [int(num) for num in input().split()]
	for x in range(0, pos):
		temp = 0
		mul = 1
		for i in range(0, 8):
			if lay[x + i] == '.':
				temp += letters[i]
			elif lay[x + i] == 'd':
				temp += letters[i] * 2
			elif lay[x + i] == 't':
				temp += letters[i] * 3
			elif lay[x + i] == 'D':
				mul *= 2
				temp += letters[i]
			else:
				mul *= 3
				temp += letters[i]
		temp *= mul
		if temp > scr:
			scr = temp
	ans.append(scr)
	t = t - 1
for x in ans:
	print(int(x))


def listin():
	a = list(map(int, input().split()))
	return a
for tastcas in range(int(input())):
	n = int(input())
	s = input()
	a = listin()
	l = 8
	maxi = 0
	for i in range(n - l + 1):
		Ds = 0
		Ts = 0
		score = 0
		for j in range(l):
			t = i + j
			if s[t] == 'd':
				score += 2 * a[j]
			elif s[t] == 't':
				score += 3 * a[j]
			else:
				score += a[j]
			if s[t] == 'D':
				Ds += 1
			if s[t] == 'T':
				Ts += 1
		score *= 2 ** Ds * 3 ** Ts
		if maxi < score:
			maxi = score
	print(maxi)


import math
import os
import random
import re
import sys

def findMaxScore(mod, arr):
	m = 0
	for i in range(0, len(mod) - 7):
		m = max(m, getScore(mod[i:i + 8], arr))
	return m

def getScore(s, arr):
	m = 1
	score = 0
	for i in range(0, 8):
		if s[i] == 'd':
			score += 2 * arr[i]
		elif s[i] == 't':
			score += 3 * arr[i]
		elif s[i] == 'D':
			score += arr[i]
			m *= 2
		elif s[i] == 'T':
			score += arr[i]
			m *= 3
		else:
			score += arr[i]
	return m * score

def __starting_point():
	try:
		n = int(input())
		for _ in range(n):
			l = int(input())
			mod = input()
			arr = [int(y) for y in input().split()]
			print(findMaxScore(mod, arr))
	except EOFError:
		x = 2
__starting_point()


def score(B, L):
	T = 0
	D = 0
	S = 0
	for (i, b) in enumerate(B):
		if b == 't':
			S += L[i] * 2
		elif b == 'd':
			S += L[i]
		elif b == 'D':
			D += 1
		elif b == 'T':
			T += 1
		S += L[i]
	return S * 2 ** D * 3 ** T
T = int(input())
for k in range(T):
	N = int(input())
	B = list(input())
	L = list(map(int, input().split()))
	V = len(B) - 8 + 1
	maxi = 0
	for l in range(V):
		tmp = score(B[l:l + 8], L)
		if tmp > maxi:
			maxi = tmp
	print(maxi)


t = int(input())
for tc in range(t):
	n = int(input())
	b = input()
	s = [int(k) for k in input().split()]
	ans = 0
	for i in range(n - 7):
		score = 0
		d = 0
		t = 0
		for j in range(8):
			if b[i + j] == '.':
				score += s[j]
			elif b[i + j] == 'd':
				score += 2 * s[j]
			elif b[i + j] == 't':
				score += 3 * s[j]
			elif b[i + j] == 'D':
				score += s[j]
				d += 1
			else:
				score += s[j]
				t += 1
		score = score * pow(2, d) * pow(3, t)
		ans = max(ans, score)
	print(ans)


testcases = int(input())
for i in range(testcases):
	N = int(input())
	board = str(input())
	scores = list(input().split(' '))
	maxAns = 0
	for i in range(N - 7):
		subBoard = board[i:i + 8]
		preAns = 0
		double = 0
		triple = 0
		multiplier = 1
		ans = 0
		for j in range(8):
			if subBoard[j] == 'd':
				preAns += int(scores[j]) * 2
			elif subBoard[j] == 't':
				preAns += int(scores[j]) * 3
			else:
				preAns += int(scores[j])
			if subBoard[j] == 'D':
				double += 1
			if subBoard[j] == 'T':
				triple += 1
		multiplier = 2 ** double * 3 ** triple
		ans = preAns * multiplier
		if ans > maxAns:
			maxAns = ans
	print(maxAns)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	a = list(map(int, input().split()))
	l = 8
	m = 0
	for j in range(n - l + 1):
		d = 0
		x = 0
		o = 0
		for k in range(l):
			c = j + k
			if s[c] == 'd':
				o += 2 * a[k]
			elif s[c] == 't':
				o += 3 * a[k]
			else:
				o += a[k]
			if s[c] == 'D':
				d += 1
			if s[c] == 'T':
				x += 1
		o *= 2 ** d * 3 ** x
		if m < o:
			m = o
	print(m)