local n = io.read("*n")
local t = {}
for i = 1, 100000 do
  t[i] = 0
end
for i = 1, n do
  local a = io.read("*n")
  t[a] = t[a] + 1
end
local ret = 0
for i = 1, 100000 do
  ret = ret + t[i] * i
end
local q = io.read("*n")
for iq = 1, q do
  local b, c = io.read("*n", "*n")
  ret = ret + (c - b) * t[b]
  print(ret)
  t[c] = t[c] + t[b]
  t[b] = 0
end
