import collections
N = int(input())
a = list(map(int, input().split()))
a_c = collections.Counter(a)
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]
S = [0]*Q

for i, bc in enumerate(BC):
  b = bc[0]
  c = bc[1]
  s = 0
  if b in a_c:
    a_c[c]+=a_c[b]
    del a_c[b]
  for k, v in a_c.items():
    s += k*v
  S[i] = s

[print(i) for i in S]