awk -M 'RS=FS{x=$2?!!(h=$2):x*$1;print x, x%h;x%=h;}END{print x}'