from collections import defaultdict

d = defaultdict(int)

N = input()
A = list(map(int, input().split())) 
for i in A:
    d[i] += 1

Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    d[C] += d[B]
    d[B] = 0
    ans = 0
    for i,j in d.items():
        ans += i*j
    print(ans)