import sys
readline = sys.stdin.buffer.readline

from collections import Counter


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

S = sum(A)
C = Counter(A)

ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    count = C[b]
    S += (c - b) * count
    C[b], C[c] = 0, C[c] + count
    ans.append(S)

print('\n'.join(map(str, ans)))