n = int(input())
a = str(input())
q = int(input())
s = []

for i in range(q):
    b,c = map(str,input().split())
    a = a.replace(b,c)
    s.append(sum(map(int,a.split())))

for k in range(q):
    print(str(s[k]))