n = int(input())
a = input()
q = int(input())
for i in range(q):
    b,c = map(str,input().split())
    if b not in a:
        print(sum(x))
        break
    a = a.replace(b,c)
    x = a.split()
    x = [int(s) for s in x]
    print(sum(x))