def func(s,x):

    last=[0]*26

    score=0

    for i,v in enumerate(x,1):

        last[v]=i

        c=0

        for j in range(26):

            c+=s[j]*(i-last[j])

        score=score+s[i*26+v]-c

        print(score)

    return score

def main():

    d,*s=list(map(int,open(0).read().split()))

    func(s,[t-1for t in s[-d:]])

main()
d,*s,=list(map(int,open(i:=0).read().split()))

c,*l=[0]*27

for v in s[-d:]:

  i+=1

  l[v-1]=i

  print((c:=c+s[i*26+v-1]-sum(x*(i-y)for x,y in zip(s,l))))
def func(s, x):
    last = [0] * 26
    score = 0
    for i, v in enumerate(x, 1):
        last[v] = i
        c = 0
        for j in range(26):
            c += s[j] * (i - last[j])
        score = score + s[i * 26 + v] - c
        print(score)
    return score


def main():
    d, *s = list(map(int, open(0).read().split()))
    func(s, [t - 1 for t in s[-d:]])


main()

(
    d,
    *s,
) = list(map(int, open(i := 0).read().split()))
c, *l = [0] * 27
for v in s[-d:]:
    i += 1
    l[v - 1] = i
    print((c := c + s[i * 26 + v - 1] - sum(x * (i - y) for x, y in zip(s, l))))

-def func(s, x): -    last = [0] * 26 -    score = 0 -    for i, v in enumerate(x, 1): -        last[v] = i -        c = 0 -        for j in range(26): -            c += s[j] * (i - last[j]) -        score = score + s[i * 26 + v] - c -        print(score) -    return score - - -def main(): -    d, *s = list(map(int, open(0).read().split())) -    func(s, [t - 1 for t in s[-d:]]) - - -main() +( +    d, +    *s, +) = list(map(int, open(i := 0).read().split())) +c, *l = [0] * 27 +for v in s[-d:]: +    i += 1 +    l[v - 1] = i +    print((c := c + s[i * 26 + v - 1] - sum(x * (i - y) for x, y in zip(s, l))))