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

s = {}

for v in a:
  if v in s:
    s[v] += 1
  else:
    s[v] = 1

for t in r:
  if t[0] in s:
    if t[1] in s:
      s[t[1]] += s[t[0]]
    else:
      s[t[1]] = s[t[0]]
    del s[t[0]]
  print(sum([i * s[i] for i in s]))
