#N,A,Q,B,C
import itertools
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
line = [0]*Q
for i in range(Q):
  line[i] = list(map(int,input().split()))
  #print(line)
#print(N,A,Q)
line2 = list(itertools.chain.from_iterable(line))
X = [0]*(max(line2)+Q+1)
#print(max(line2))
total = 0
for i in range(N):
  X[A[i]-1] = A.count(A[i])
  total += A[i]

for i in range(Q):
  #print(B,C)
  total += (line[i][1]-line[i][0])*X[line[i][0]-1]
  X[line[i][1]-1] += X[line[i][0]-1]
  X[line[i][0]-1] = 0
  print(total)