from operator import mul
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    k = list(cnt.keys())
    v = list(cnt.values())
    print(sum(map(mul, k, v)))