N=int(input())
A=str(input())
Q=int(input())
BC=[list(input().split()) for _ in range(Q)]

for i in range(Q):
    if BC[i][0] in A:
        A=A.replace(BC[i][0],BC[i][1])
        B=A.split(' ')
        B_i = [int(s) for s in B]
    print(sum(B_i))
