local read = io.read
local insert = table.insert

local N = read("*n")
local key_A = {}
for i = 1, N do
	local A = read("*n")
	if key_A[A] then
		key_A[A] = key_A[A] + 1
	else
		key_A[A] = 1
	end
end

local total = 0
for key, value in pairs(key_A) do
	total = total + key * value
end


local out = {}
local Q = read("*n")
for i = 1, Q do
	local B, C = read("*n", "*n")
	if key_A[B] and key_A[C] then
		total = total - B*key_A[B] + C*key_A[B]
		key_A[C] = key_A[C] + key_A[B]
		key_A[B] = nil
	elseif key_A[B] then
		total = total - B*key_A[B] + C*key_A[B]
		key_A[C] = key_A[B]
		key_A[B] = nil
	end
	out[i] = total
end

print(table.concat(out, "\n"))
