import re
N = int(input())
A = input()
Q = int(input())
for i in range(Q):
    B,C = [str(x) for x in input().split()]
    A = re.sub(B,C,A)
    results = [int(x) for x in A.split()]
    print(sum(results))