n = int(input())
a = input()
q = int(input())
l = [list(map(str, input().split())) for _ in range(q)]

for i in range(q):
    if l[i][0] in a:
        a = a.replace(l[i][0], l[i][1])
        new_a = a.split()
        new_a = [int(j) for j in new_a]
    print(sum(new_a))