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])
