// https://atcoder.jp/contests/abc156/tasks/abc156_f
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

fn inv(a: i64, modulo: i64) -> i64 {
    pow(a, modulo - 2, modulo)
}

fn pow(a: i64, p: i64, modulo: i64) -> i64 {
    let mut p = p;
    let mut aa = a;
    let mut ret = 1;
    while p >= 1 {
        if p & 1 == 1 {
            ret *= aa;
            ret %= modulo;
        }
        p >>= 1;
        aa = aa * aa % modulo;
    }
    ret
}


//===

fn count_from(d: &Vec<i64>, till: usize, mut from: i64, modulo: i64) -> i64 {
    let mut count = 0;
    for i in 0..till {
        let di = d[i];
        let to = (from + di) % modulo;
        if from < to {
            count += 1;
        }
        from = to;
    }
    count
}

fn jump(m: i64, jpl: i64, lp: i64) {
    let mut stat = vec![0; m as usize];
    for i in 0..jpl {
        stat[(m-1-i) as usize] = 1;
    }

    let mut ans = vec![0; m as usize];
    for i in 0..m {
        let mut now = i;
        let mut counter = 0;
        for _ in 0..lp {
            counter += stat[now as usize];
            now = (now + jpl) % m;
        }
        ans[i as usize] = counter;
    }
    debug!(ans);
}

fn solve(d: &Vec<i64>, n: i64, mut x: i64, m: i64) -> i64 {
    x %= m;
    let dn = d.len();
    let mut jump_per_loop = 0;
    for i in 0..dn {
        jump_per_loop += d[i] % m;
    }
    jump_per_loop %= m;

    let need_loop = (n - 1) / (dn as i64);
    let last_x = (x + need_loop * jump_per_loop) % m;
    let mut answer = count_from(d, ((n - 1) % (dn as i64)) as usize, last_x, m);
    let mut from = (0, count_from(d, dn, 0, m));
    let mut to = (m-1, count_from(d, dn, m-1, m));

    if jump_per_loop == 0 {
        // always in the same state
        return answer + need_loop * from.1;
    }
    assert!(from.1 == to.1 + 1);

    // while to.0 - from.0 > 1 {
    //     let med = (to.0 + from.0) / 2;
    //     if count_from(&d, dn, med, m) == from.1 {
    //         from.0 = med;
    //     } else {
    //         to.0 = med;
    //     }
    // }
    // assert!(to.0 + jump_per_loop == m);

    answer += need_loop * from.1;
    let movaway = need_loop * jump_per_loop;
    let x = ifv!(x == 0, m, (m - x) % m);
    if movaway >= x {
        answer -= 1;
        answer -= (movaway - x) / m;
    }
    answer
}

fn main() {
    // jump(103, 7, 114514);

    input! {
        k: usize, q: usize,
        d: [i64; k],
        queries: [(i64, i64, i64); q]
    };

    for (n, x, m) in queries {
        println!("{}", solve(&d, n, x, m));
    }
}


Measured memory_bytes: 4352