n = int(input())
alist = [int(s) for s in input().split(" ")]
adict = {}
for a in alist:
  if a in adict:
    adict[a] += 1
  else:
    adict[a] = 1
q = int(input())
bclist = []
for _ in range(q):
  b, c = [int(s) for s in input().split(" ")]
  bclist.append([b, c])

for b, c in bclist:
  if c in adict and b in adict:
    adict[c] += adict[b]
    del adict[b]
  elif b in adict:
    adict[c] = adict[b]
    del adict[b]
  ans = 0
  for k, v in adict.items():
    ans += k * v
  print(ans)