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

ans = sum(A)
unko = [0]*10000
for i in A:
    unko[i-1] += 1

for i in range(Q):
    B, C = map(int, input().split())

    a = unko[B-1]

    ans = ans + (C-B)*a

    unko[B-1] = 0
    unko[C-1] += a

    print(ans)

