N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B=[list(map(int, input().split())) for _ in range(Q)]
D={}
ma=0
for i in A:
  ma+=i
  if i not in D:
    D[i]=1
  else:
    D[i]+=1
    
for b,c in B:
  if b in D:
    f=D[b]
    t=D[c]
    D[c]=f+t
    D[b]=0
    ma+=(c-b)*f
    print(ma)