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 range(input()):
    cost = map(int, raw_input().split())
    for x in range(10):
        for y in range(1, 10):
            for z in range(y, 10):                
                cost[(y + z) % 10] = min(cost[(y + z) % 10], cost[y]+cost[z])
               
    input()
    ans = 0
    nums = raw_input()
    for  x in nums:
        ans += cost[int(x)]
    print ans