from collections import defaultdict
n = int(input())
A=list(map(int,input().split()))
d = defaultdict(int)
for a in A:
  d[a]+=1
for _ in range(int(input())):
  b,c=map(int,input().split())
  d[c] += d[b]
  ans = 0
  for a,b in d.items():
    ans += a*b
  print(ans)