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

from collections import defaultdict
dic = defaultdict(int)
for a in A:
  dic[a] += 1 
  
ans = sum(A)
for i in range(q):  
  b,c = map(int,input().split())
  dic[c] += dic[b]
  diff = dic[c]*c-dic[b]*b
  ans += diff
  print(ans)  
  dic[b] = 0    