N = int(input())
A = list(map(int,input().split()))
Q = int(input())
bcs = []
for i in range(Q):
  j,k = map(int,input().split())
  bcs.append([j,k])
cnts = [0]*100000
for a in A:
  cnts[a-1]+=1
for b,c in bcs:
  t = cnts[b-1]
  cnts[b-1]=0
  cnts[c-1]+=t
  s = 0
  for i in range(1,100001):
    s += cnts[i-1]*i
  print(s)