N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bucket = [0]*100001

for i in range(100000):
    bucket[i] = A.count(i)

sum = sum(A)

for i in range(Q):
    b, c = list(map(int, input().split()))
    sum += (c - b) * bucket[b]
    bucket[c] += bucket[b]
    bucket[b] = 0
    print(sum)