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)
#print(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  s=0
  idx=[]
  for jj in range(len(a2)):
    if a2[jj][0]==b or a2[jj][0]==c:
      idx.append(jj)
      s+=a2[jj][1]
      a2[jj][0]=0
      a2[jj][1]=0
  a2.append([c,s])
  #print(a2)
  r=0
  for jj in range(len(a2)):
     r+= a2[jj][0]*a2[jj][1]
  print(r)
