from collections import Counter

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

Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]

d = Counter(A)

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

    li = [k * v for k, v in d.items()]
    print(sum(li))
