read N
read str
read Q

calc=$(cnum=$((N-1));printf "%${cnum}s\n" | sed "s; ; + ;g")" p"


for qnum in `seq  $Q`; do
	read b c
	newstr=$(echo $str | tr " " "\n" | sed -e "s/$b/$c/g" | tr "\n " " " | sed "s;.$;;g" )
    echo ${newstr}${calc} | dc
    str=$newstr
done