N = int(input())
arrange = input()
Q = int(input())


for i in range(Q):
    task = input().split()
    arrange = arrange.replace(task[0], task[1])
    answer = sum([int(i) for i in arrange.split()])
    print(answer)
