n = input()
a = input()
q = int(input())
bb = []
cc = []
for i in range(q):
    btmp, ctmp = input().split()
    bb.append(btmp)
    cc.append(ctmp)

for b,c in zip(bb,cc):
    a = a.replace(b,c)
    print(sum([int(j) for j in a.split()])) 
