import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

import collections

buk = [0] * (10**5 + 1)
for e in a:
  buk[e] += 1


# dprint(buk)

for b, c in bc:
  sum = 0
  for k in range(10 ** 5 + 1):
    if buk[k] == 0:
      continue
    if k == b:
      # dprint(b, c)
      buk[c] += buk[b]
      buk[b] = 0
      # dprint(buk)
      sum += c * buk[c]
    else:
      if k != c:
        sum += k * buk[k]
    # dprint('sum', '=', sum)
  print(sum)
