# from collections import defaultdict
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
s = sum(A)
store = dict()
for q in range(Q):
    count = 0
    B,C = [int(x) for x in input().split()]
    if B not in store:
        store[B]=A.count(B)
    if C not in store:
        store[C]=A.count(C)
    s = s - store[B]*B + store[B]*C
    store[C] += store[B]
    store[B] = 0
    print(s)
    # print(store)

    