N = int(input())
A = str(input())
Q = int(input())
sumLi = []
for i in range(Q):
    B, C = map(str, input().split())
    A = A.replace(B, C)
    nA = list(map(int, A.split()))
    sumLi.append(sum(nA))
for j in range(len(sumLi)):
    print(sumLi[j])
