n=int(input())

a=list(map(int,input().split()))

z=[0]*((10**5)+1)

for i in range(n):
  z[a[i]]+=1

q=int(input())

y=[]


for i in range(q):
  b,c=map(int,input().split())
  z[c]+=z[b]
  z[b]=0
  #print(z)
  sam=0
  for j in range(len(z)):
    sam+=z[j]*j
  y.append(sam)

for i in range(len(y)):
  print(y[i])