N = int(input())
A = input()
# A = list(map(int, input().split()))
Q = int(input())
t = []

for i in range(Q):
    b, c = map(str, input().split())
    A=A.replace(b, c)
    A2 = list(map(int, A.split()))
    t.append(sum(A2))

for _ in t:
    print(_)