fn main() { let t = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse::().unwrap() }; for _ in 0..t { println!("{}", solve()); } } fn solve() -> usize { let m = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse::().unwrap() }; let dd: Vec<_> = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|x| x.parse::().unwrap()) .collect() }; if dd.iter().all(|&d| d == 0) { return m; } let mut upper_digits = 0; for (i, &d) in dd.iter().enumerate() { for _ in 0..d { upper_digits = 10 * upper_digits + i + 1; } } upper_digits *= 10_usize.pow(9); upper_digits + (m - upper_digits % m) % m }