n = int(input())

a = input()
#print(a)
q = int(input())

b = []
for _ in range(q):
    b.append(input().split())
#print(b)
for i in range(q):
    a.replace(b[i][0],b[i][1],100000)
    c = 0
    d = a.split(" ")
    #print(d)
    for j in range(len(d)):
        c += int(d[j])
    print(c)
