from collections import Counter
N = int(input())
A = list(map(int,input().split()))
Q = int(input())

num = Counter(A)

for i in range(Q):
  b,c = map(int,input().split())
  num[c] += num[b]
  num[b] = 0
  ans = 0
  for i in num:
      ans += i * num[i] 
  print(ans)