read n
read -a a
x=(`yes 0 | head -100001`)
s=$((`echo ${a[@]} | tr ' ' '+'`))
for i in ${a[@]}
do
    x[$i]=$((x[i]+1))
done
read q
for i in `seq $q`
do
    read b c
    s=$((s+x[b]*(c-b)))
    x[$c]=$((x[b]+x[c]))
    x[$b]=0
    echo $s
done
