use std::io::*; const MOD: i64 = 1_000_000_007; 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, mut b: i64, mut c: i64, y: i64) -> i64 { let mut ans = 0; let mut x0 = 0; let mut y0 = 0; let g = ext_gcd(b, c, &mut x0, &mut y0); b /= g; c /= g; for i in 0..=y / a { let mut d = y - i * a; if d % g != 0 { continue; } d /= g; // d = b * x + c * y // b(x - x0) = -c(y - y0) // x = x0 + c*i >= 0 // y = y0 - b*i >= 0 // -x0 / c <= i <= y0 / b if x0 < y0 { std::mem::swap(&mut x0, &mut y0); std::mem::swap(&mut b, &mut c); } ans = (ans + (x0 * d) / c - (-y0 * d + b - 1) / b + 1) % 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 mut a = vec![0; 3]; a[0] = itr.next().unwrap().parse().unwrap(); a[1] = itr.next().unwrap().parse().unwrap(); a[2] = itr.next().unwrap().parse().unwrap(); let y: i64 = itr.next().unwrap().parse().unwrap(); a.sort_by_key(|x| -x); println!("{}", calc(a[0], a[1], a[2], y)); } }