main=do
 getLine
 d<-zip[2..]$map read.words<$>getLine
 let k=length d
 ans<-unlines.map(show.(\[n,x,m]->n-1-div(x`mod`m+sum[(m-(-e)`mod`m)*div(n-i+k)k|(i,e)<-d])m).map read.words).lines<$>getContents
 putStr ans