import Control.Monad

main = do
  [k, q] <- map read.words <$> getLine
  d <- map read.words <$> getLine
  nxm <- replicateM q $ map read.words <$> getLine
  mapM_ (print.solve k d) nxm

solve :: Int -> [Int] -> [Int] -> Int
solve k d' [n, x, m] = n - 1 - eqs - gts where
  d = map (`rem` m) d'
  eqs = length (filter (==0) d) * (quot (n-1) k) + length (filter (==0) (take (rem (n-1) k) d))
  gts = fst alast - quot x m
  alast = foldl (+%) (quotRem x m) dcnt
  cnt i | i < rem (n-1) k = div (n-1) k + 1
        | otherwise = div (n-1) k
  dcnt = zipWith f d $ map cnt [0..]
  f a b = quotRem a m *% quotRem b m
  (a, x) +% (b, y) = (a + b + quot (x + y) m, rem (x + y) m)
  (a, x) *% (b, y) = (a * b * m + a * y + b * x + quot (x * y) m, rem (x * y) m)