N = int(input())
A = list(map(int, input().split()))
Q = int(input())
answer = []
counter = [0] * (10**5)
for a in A:
    counter[a] += 1
for _ in range(Q):
    B, C = map(int, input().split())
    counter[C] += counter[B]
    counter[B] = 0
    hoge = [idx*num for idx, num in enumerate(counter)]
    answer.append(sum(hoge))
for res in answer:
    print(res)