N=int(input())
A=str(input())
Q=int(input())
BC=[list(input().split()) for _ in range(Q)]
B_i=[0]*N
for i in range(Q):
    if BC[i][0] in set(A):
        A=A.replace(BC[i][0],BC[i][1])
        B_i = [int(s) for s in A.split(' ')]
    print(sum(B_i))
