N=int(input())
A=list(map(int,input().split()))
Q=int(input())
D=dict()
for i in A:
  if i in D:
    D[i]+=1
  else:
    D[i]=1
for i in range(Q):
  b,c=map(int,input().split())
  if b in D:
    if c in D:
      D[c]+=D[b]
    else:
      D[c]=D[b]
    D[b]=0
  r=0
  for i,j in D.items():
    r+=i*j
  print(r)