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

def solve(l,n,p):
    b = True
    while b:
        b = False
        for i in xrange(10):
            for j in xrange(10):
                if l[i] + l[j] < l[(i+j)%10]:
                    b = True
                    l[(i+j)%10] = l[i] + l[j]

    r = 0
    for x in p:
        r += l[x]

    return r

t = int(raw_input())
for i in xrange(t):
    l = map(int,raw_input().strip().split())
    n = int(raw_input())
    p = map(int,raw_input())
    print solve(l,n,p)