# 入力
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]


array = [0]*(10**5+1)

for a in A:
    array[a] += 1

for bc in BC:
    b, c = bc
    array[c] += array[b]
    array[b] = 0

    total = 0
    for i in range(10 ** 5 + 1):
        total += i * array[i]
    print(total)
