N = int(input())
nums = [int(s) for s in input().rstrip().split()]
Q = int(input())

dct = {}
for k in range(N):
  n = nums[k]
  if n in dct:
    dct[n] += 1
  else:
    dct[n] = 1

for _ in range(Q):
  B, C =(int(s) for s in input().rstrip().split())
  if B in dct:
    if C in dct:
      dct[C] += dct.pop(B)
    else:
      dct[C] = dct.pop(B)

  sum = 0
  for k, v in dct.items():
    sum += k * v
  
  print(sum)
