n = int(input())
a = input()

q = int(input())
tmp = []
for i in range(q):
    bc = input().split()
    tmp.append(bc)

for j in range(len(tmp)):
    a = a.replace(tmp[j][0], tmp[j][1])
    answer = a.split()
    answer_int = [int(x) for x in answer]
    print(sum(answer_int))
