3
3 2 2 1 1 1 2 3 1 2 
1
3
1 2 3 2 1 2 2 3 1 1 
2
98
2 1 3 3 1 2 3 1 3 3 
1
1 3
7 3 2 1 2 4 7 7 5 9 
10
1385294192
3 6 1 2 7 3 10 7 2 8 
7
8129080
1 5 7 3 1 1 9 9 8 10 
6
540120
1
2
1
 26
20
12

for _ in xrange(input()):
	count=min=pi=pj=0
	cost=map(int,raw_input().split())
	n=input()
	num=raw_input().strip()
	for rep in range(10):
		for i in xrange(10):
			for j in xrange(10):
				d=(i+j)%10
				if cost[i]+cost[j]<cost[d]:
					cost[d]=cost[i]+cost[j]
	for i in xrange(n): count+=cost[int(num[i])]
	print count