# ####################################################################
# import io
# import sys

# _INPUT = """\
# 2
# 1 2
# 3
# 1 100
# 2 100
# 100 1000
# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################


N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

#「groupby」を使って、
from itertools import groupby
L = [[k, len(list(g))] for k, g in groupby(A)]
# print( L )

from collections import defaultdict
d = defaultdict(int)
for k,v in L:
  d[k] = v

# print(d)

sumA = sum(A)

for q in range(Q):
  b,c = map(int, input().split())

  k = d[b]
  sumA += (c-b)*k
  print(sumA)

  d[c] += d[b]
  d[b] = 0




