n=int(input())
a=list(map(int,input().split()))
q=int(input())
x=[]

a.sort()
old=0
count=-1
for i in range(n):
  if a[i]!=old:
    x.append([a[i],1])
    count=count+1
  else:
  	x[count][1]=x[count][1]+1
  old=a[i]
for i in range(q):
  xlen=len(x)
  flag=0
  x.sort()
  s=0
  b,c=map(int,input().split())
  for j in range(xlen):
    if x[j][0]==b:
      c1=x[j][1]
      del(x[j])
      flag=1
      break
  xlen=len(x)
  for j in range(xlen):
    if x[j][0]==c:
      x[j][1]=x[j][1]+c1
      flag=2
      break
  if flag==1:
    x.append([c,c1])
  xlen=len(x)
  for j in range(xlen):
    s=s+x[j][0]*x[j][1]
  print(s)