n=int(input())
r=list(map(int,input().split()))
m={}
for u in r:
  if u not in m:m[u]=0
  m[u] += 1
s=0
for k in m:s+=k*m[k]
for _ in range(int(input())):
  a,b=map(int,input().split())
  if a in m:
    s += (b - a) * m[a]
    m[b] += m[a]
    del m[a]
  print (s)