N = int(input())
A =[0] * 10001
s = 0
for i in map(int, input().split()):
    A[i] += 1
    s += i

K = int(input())

for i in range(K):
    f, t = map(int, input().split())
    A[t] += A[f]
    s += t * A[f]
    s -= f * A[f]
    A[f] = 0
    print(s)