n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]]+=1
  else:
    dic[a[i]]=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  if b in dic:
    if c in dic:
      dic[c]+=dic[b]
      del dic[b]
    else:
      dic[c]=dic[b]
      del dic[b]
  ans=0
  for key,value in dic.items():
    ans+=key*value
  print(ans)