N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B_to_C = []
for i in range(Q):
  B_to_C.append([int(x) for x in input().split()])

dict_A = {}

for a in A:
  if a in dict_A:
    dict_A[a] += 1
  else:
    dict_A[a] = 1
    
for B, C in B_to_C:
  if B in dict_A:
    if C in dict_A:
      dict_A[C] += dict_A[B]
      del dict_A[B]
    else:
      dict_A[C] = dict_A[B]
      del dict_A[B]
      
  sums = 0
  for k, v in dict_A.items():
    sums += k*v
  print(sums)
  
  