N = int(input())
A = str(input())
Q = int(input())
sumLi = []
i = 0
j = 0
while i < Q:
    B, C = map(str, input().split())
    A = A.replace(B, C)
    nA = list(map(int, A.split()))
    sumLi.append(sum(nA))
    i += 1
while j < Q:
    print(sumLi[j])
    j += 1
