n = int(input())
a = input().replace(' ', '')

q = int(input())
tmp = []
for i in range(q):
    bc = input().split()
    b = bc[0]
    c = bc[1]
    tmp.append([b, c])

for j in range(len(tmp)):
    a = a.replace(tmp[j][0], tmp[j][1])
    answer = ''
    for k in range(len(a)):
        answer += a[k]
        answer += ' '
    answer_list = answer.split()
    answer_list = [int(x) for x in answer_list]
    print(sum(answer_list))