n = int(input())
a = list(map(int,input().strip().split()))
q = int(input())

import collections

counter = collections.Counter(a)

result = 0
for j in range(q):
  b,c = list(map(int,input().strip().split()))
  if c in counter:
    counter[c] += counter[b]
  else:
    counter[c] = counter[b]
  counter[b] = 0
  result = 0
  for i in counter:
    result += i * counter[i]
  print(result)