import collections

N=int(input())
A=[int(_) for _ in input().split(" ")]
Q=int(input())

c = collections.Counter(A)


#d['k10'] = d.pop('k1')    
    
for i in range(Q):
  B,C=map(int,input().split(" "))
  
  if C in c:
    c[C] += c[B]
    del c[B]
  else:
    c[C] = c[B]
    del c[B]
  
  #print(c)
  
  s=list(c.keys())
  j=0
  ans=0
  for v in c.values():
    ans += s[j]*v
    j=j+1
  
  print(ans)


  
  