N = int(input())
A = list(map(int, input().split()))
Q = int(input())

dic = {a:0 for a in A}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  if C not in dic.keys():
    dic[C] = 0
  if B in dic.keys():
    dic[C] += dic[B]
    dic[B] = 0
  
  ans = 0
  for k, v in dic.items():
    ans += k*v
  
  print(ans)