n=int(input())
a=input()
q=int(input())
BC=[[] for _ in range(q)]
for i in range(q):
    b,c=input().split()
    BC[i].append(b)
    BC[i].append(c)
for i in range(q):
    if BC[i][0] in a:
        
        a=a.replace(BC[i][0],BC[i][1])
    e=list(map(int,a.split()))
    print(sum(e))