import sys
input = sys.stdin.buffer.readline
from collections import Counter
n=int(input())
*a,=map(int,input().split())
C=Counter(a)
ans=sum(a)
for _ in range(int(input())):
	b,c=map(int,input().split())
	ans+=C[b]*(c-b)
	C[c]+=C[b]
	C[b]=0
	print(ans)