n = int(input())
a = list(map(int, input().split()))
q = int(input())
num_list = [0 for i in range(100001)]
for i in a:
    num_list[i] += 1
ans = 0
for i in range(100001):
    ans += i * num_list[i]

for _ in range(q):
    b, c = map(int, input().split())
    num_list[c] += num_list[b]
    ans += num_list[b] * (c - b)
    print(ans)