N = int(input())

A = input()

Q = int(input())

sumlist = []

for i in range(Q):
    B, C = input().split()
    A = A.replace(B,C)
    A_copy = list(map(int,A.split()))
    sumlist.append(sum(A_copy))
    

for k in range(len(sumlist)):
    print(sumlist[k])