N=io.read"*n"
t={}
for i=1,N do
	n=io.read"*n"
	t[n]=(t[n]or 0)+1
end
Q=io.read"*n"

for i=1,Q do
	B=io.read"*n"
	C=io.read"*n"
	
	t[C]=(t[C]or 0)+(t[B]or 0)
		
	t[B]=0
	
	s=0
	for k,v in pairs(t)do
		s=s+k*v
	end
	print(s)

end