n=int(input())
a=list(map(int,input().split()))
sa=set(a)
a2=[]
for ii in sa:
  ca=a.count(ii)
  a2.append([ii,ca])
  for jj in range(ca):
    a.remove(ii)
import numpy as np
a2=np.array(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  idx=np.where(a2[:,0]==b)
  idx=np.append(idx,np.where(a2[:,0]==c))
  s=0
  for jj in idx:
    s+=a2[jj,1]
  a2=np.delete(a2,idx,axis=0)
  a2=np.append(a2,[[c,s]],axis=0)
  print(sum(a2[:,0]*a2[:,1]))
