import math

input_num = int(input())
input_numline = list(map(int,input().split(' ')))
q = int(input())

op_list = []
for i in range(q):
    op_list.append(list(map(int,input().split(' '))))

maxnum = 100000

num_list = [0 for m in range(maxnum+1)]

for n in input_numline:
    num_list[n] += 1

for o in op_list:
    num_list[o[1]] += num_list[o[0]]
    num_list[o[0]] = 0

    ans = 0
    for j in range(maxnum+1):
        ans += j*num_list[j]
    print(ans)
