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