from collections import defaultdict

N = int(input())
d = defaultdict(int)
A = list(map(int, input().split()))
for a in A:
    d[a] += 1
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    d[C] += d[B]
    del d[B]
    ans = 0
    for k, v in d.items():
        ans += k * v
    print(ans)