read N
read str
read Q

for qnum in `seq  $Q`; do
	read b c
	newstr=$(echo $str | sed "s;^$b;$c;g;  s;$b$;$c;g; s; $b ; $c ;g")
	echo $newstr | tr " " "\n" | awk -v sum=0 '{sum+=$0}END{print sum}'
    str=$newstr
done