from collections import defaultdict

N = int(input())
nums = list(map(int,input().split()))
Q = int(input())

dct = defaultdict(int)
for _ in range(N):
  dct[nums.pop()] += 1

for _ in range(Q):
  B, C = map(int,input().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)
