use std::io::*; const MOD: i64 = 1_000_000_07; fn ext_gcd(a: i64, b: i64, x: &mut i64, y: &mut i64) -> i64 { if b == 0 { *x = 1; *y = 0; a } else { let d = ext_gcd(b, a % b, y, x); *y -= (a / b) * *x; d } } fn calc(a: i64, b: i64, c: i64, y: i64) -> i64 { let mut ans = 0; for i in 0..=y / a { let d = y - i * a; // d = b * x + c * y let mut x0 = 0; let mut y0 = 0; let g = ext_gcd(b, c, &mut x0, &mut y0); if d % g != 0 { continue; } x0 *= d / g; y0 *= d / g; let bs = b / g; let cs = c / g; if x0 < 0 { // while x0 < 0 { // x0 += cs; // y0 -= bs; // } let u = (-x0 + cs - 1) / cs; y0 -= u * bs; // while y0 >= 0 { // ans += 1; // y0 -= bs; // } if y0 >= 0 { ans = (ans + (y0 / bs + 1) % MOD) % MOD; } } else { // while y0 < 0 { // y0 += bs; // x0 -= cs; // } let u = (-y0 + bs - 1) / bs; x0 -= u * cs; // while x0 >= 0 { // ans += 1; // x0 -= cs; // } if x0 >= 0 { ans = (ans + (x0 / cs + 1) % MOD) % MOD } } } ans } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); for _ in 0..t { let n: i64 = itr.next().unwrap().parse().unwrap(); let k: i64 = itr.next().unwrap().parse().unwrap(); let h: i64 = itr.next().unwrap().parse().unwrap(); let y: i64 = itr.next().unwrap().parse().unwrap(); let nc = y / n; let kc = y / k; if nc <= 1000000 { println!("{}", calc(n, k, h, y)); } else if kc <= 1000000 { println!("{}", calc(k, h, n, y)); } else { println!("{}", calc(h, n, k, y)); } } }