from collections import defaultdict as dd

def c(it):
  counter = dd(int)
  for x in it:
    counter[x] += 1
  return dict(counter)

n=int(input())
a=sorted((int(x) for x in input().split()))
q=int(input())

d=c(a)

for _ in range(q):
  b,c=(int(x) for x in input().split())
  num=d.pop(b,0)
  d.setdefault(c,0)
  d[c]+=num
  
  s=0
  for k,v in d.items():
    s+=k*v
  print(s)
