N = int(input())
A = map(int, input().split())
Q = int(input())

dic = [0] * (10 ** 5 + 1)
for x in A:
    dic[x] += 1

for i in range(Q):
    B, C = map(int, input().split())
    dic[C] += dic[B]
    dic[B] = 0
    sum = 0
    for j, x in enumerate(dic):
        sum += j * x
    print(sum)
