from collections import Counter


n = int(input())
a = list(map(int, input().split()))
counter = Counter(a)
ini_sum = sum(a)
q = int(input())
s = [0] * q

for i in range(q):
    b, c = map(int, input().split())
    counter[c] += counter[b]
    counter[b] = 0
    print(sum(key * value for key, value in counter.items()))
