Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*(q+1)
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],[])
for i in range(n):
	f[a[i]].append(i)
for g,h in f.items():
	#print(len(h)*g)
	s[0]=s[0]+len(h)*g
#print(f)
for i in range(1,q+1):
	if b[i-1] in f:
		if c[i-1] in f:
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-len(f[b[i-1]])*b[i-1]
			#print(s[i])
			
			s[i]=s[i]+len(f[b[i-1]])*c[i-1]
			f.pop(b[i-1])
		else:
			f.setdefault(c[i-1],[])
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-len(f[b[i-1]])*b[i-1]
			#print(s[i])
			
			s[i]+=len(f[b[i-1]])*c[i-1]
			f.pop(b[i-1])
	#print(f)
	#for g,h in f.items():
		#print(len(h)*g)
		#s[i]=s[i]+len(h)*g
	else:
		s[i]=s[i-1]
	print(s[i])


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*(q+1)
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],0)
for i in range(n):
	f[a[i]]+=1
for g,h in f.items():
	#print(len(h)*g)
	s[0]=s[0]+h*g
#print(f)
for i in range(1,q+1):
	if b[i-1] in f:
		if c[i-1] in f:
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-f[b[i-1]]*b[i-1]
			#print(s[i])
			
			s[i]=s[i]+f[b[i-1]]*c[i-1]
			f.pop(b[i-1])
		else:
			f.setdefault(c[i-1],0)
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-f[b[i-1]]*b[i-1]
			#print(s[i])
			
			s[i]+=f[b[i-1]]*c[i-1]
			f.pop(b[i-1])
	#print(f)
	#for g,h in f.items():
		#print(len(h)*g)
		#s[i]=s[i]+len(h)*g
	else:
		s[i]=s[i-1]
	print(s[i])

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*(q+1)
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],[])
for i in range(n):
	f[a[i]].append(i)
for g,h in f.items():
	#print(len(h)*g)
	s[0]=s[0]+len(h)*g
#print(f)
for i in range(1,q+1):
	if b[i-1] in f:
		if c[i-1] in f:
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-len(f[b[i-1]])*b[i-1]
			#print(s[i])
			
			s[i]=s[i]+len(f[b[i-1]])*c[i-1]
			f.pop(b[i-1])
		else:
			f.setdefault(c[i-1],[])
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-len(f[b[i-1]])*b[i-1]
			#print(s[i])
			
			s[i]+=len(f[b[i-1]])*c[i-1]
			f.pop(b[i-1])
	#print(f)
	#for g,h in f.items():
		#print(len(h)*g)
		#s[i]=s[i]+len(h)*g
	else:
		s[i]=s[i-1]
	print(s[i])


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*(q+1)
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],0)
for i in range(n):
	f[a[i]]+=1
for g,h in f.items():
	#print(len(h)*g)
	s[0]=s[0]+h*g
#print(f)
for i in range(1,q+1):
	if b[i-1] in f:
		if c[i-1] in f:
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-f[b[i-1]]*b[i-1]
			#print(s[i])
			
			s[i]=s[i]+f[b[i-1]]*c[i-1]
			f.pop(b[i-1])
		else:
			f.setdefault(c[i-1],0)
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-f[b[i-1]]*b[i-1]
			#print(s[i])
			
			s[i]+=f[b[i-1]]*c[i-1]
			f.pop(b[i-1])
	#print(f)
	#for g,h in f.items():
		#print(len(h)*g)
		#s[i]=s[i]+len(h)*g
	else:
		s[i]=s[i-1]
	print(s[i])

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*q
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],[])
for i in range(n):
	f[a[i]].append(i)
#print(f)
for i in range(q):
	if b[i] in f:
		if c[i] in f:
			f[c[i]]=f[c[i]]+f[b[i]]
			f.pop(b[i])
		else:
			f.setdefault(c[i],[])
			f[c[i]]=f[c[i]]+f[b[i]]
			f.pop(b[i])
	#print(f)
	for g,h in f.items():
		#print(len(h)*g)
		s[i]=s[i]+len(h)*g
	print(s[i])


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*(q+1)
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],0)
for i in range(n):
	f[a[i]]+=1
for g,h in f.items():
	#print(len(h)*g)
	s[0]=s[0]+h*g
#print(f)
for i in range(1,q+1):
	if b[i-1] in f:
		if c[i-1] in f:
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-f[b[i-1]]*b[i-1]
			#print(s[i])
			
			s[i]=s[i]+f[b[i-1]]*c[i-1]
			f.pop(b[i-1])
		else:
			f.setdefault(c[i-1],0)
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-f[b[i-1]]*b[i-1]
			#print(s[i])
			
			s[i]+=f[b[i-1]]*c[i-1]
			f.pop(b[i-1])
	#print(f)
	#for g,h in f.items():
		#print(len(h)*g)
		#s[i]=s[i]+len(h)*g
	else:
		s[i]=s[i-1]
	print(s[i])