import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = collections.Counter(a)
ans = []
for i in range(q):
  b, c = map(int, input().split())
  if b in dic and c in dic:
    dic[c] += dic[b]
    dic.pop(b)
  elif b in dic and c not in dic:
    dic[c] = dic[b]
    dic.pop(b)
  result = 0
  for k, v in dic.items():
    result += k * v
  ans.append(result)
for i in range(q):
  print(ans[i])