n = int(input())
a = input()
q = int(input())
for i in range(q):
    b, c = map(str, input().split())
    for j in range(a.count(b)):
        a = a.replace(b, c)
    s = list(map(int, a.split()))
    print(sum(s))