n = int(input())
#a = list(map(int,input().split()))
a = input()
q = int(input())
for i in range(q):
    b,c = input().split()
    a = a.replace(b,c)
    al = list(map(int,a.split()))
    #print(al,type(al))
    su = sum(al)
    print(su)
    