N = int(input())
A = list(map(int, input().split()))
S = sum(A)
Ct = [0] * 100001
for i in A:
    Ct[i] += 1
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    S += (C - B) * Ct[B]
    Ct[C] += Ct[B]
    Ct[B] = 0
    print(S)
