N = input()
A = input()
Q = int(input())
BC = [input().split() for _ in range(Q)]

for i in range(Q):
    A = A.replace(BC[i][0], BC[i][1])
    num_list = list(map(int, A.split()))
    print(sum(num_list))