import collections

N = int(input())
A = list(map(int,input().split()))
Q = int(input())

c=collections.Counter(A)
S = sum(A)

for i in range(Q):
    B,C = map(int,input().split())
    S += (C-B) * c[B]
    c[C] = c[C] + c[B]
    c[B] = 0
    print(S)