fn main() { let stdin = std::io::read_to_string(std::io::stdin().lock()).unwrap(); let mut stdin = stdin.split_ascii_whitespace(); unsafe { read!(stdin -> (t: u32)); let cases = (0..t) .map(|_| { read!(stdin -> (n: u32, s: u32, a: Vec[u32; n])); (s, a) }) .collect::>(); write!(output(solve(cases))); } } fn solve(cases: Vec<(u32, Vec)>) -> Vec { cases .into_iter() .map(|(s, a)| { let mut bs = mylib::BitSet::new((s + 1) as usize); bs.set(0, true); a.into_iter().for_each(|a| bs.bitor_assign_self_shl(a)); s - bs.leading_zeros() as u32 }) .collect() } fn output(ans: Vec) -> String { format_vec!(ans, "\n", (x) -> ("{}", x)) } mod mylib { pub struct BitSet { length: usize, data: Vec, } impl BitSet { #[allow(unused)] pub fn new(len: usize) -> Self { Self { length: len, data: vec![0; (len + u64::BITS as usize - 1) / u64::BITS as usize], } } #[allow(unused)] pub fn get(&self, idx: usize) -> Option { match idx < self.length { true => Some( self.data[idx / u64::BITS as usize] & (1 << (idx % u64::BITS as usize)) == 1, ), false => None, } } #[allow(unused)] pub fn set(&mut self, idx: usize, value: bool) { if idx < self.length { match value { true => self.data[idx / u64::BITS as usize] |= 1 << (idx % u64::BITS as usize), false => { self.data[idx / u64::BITS as usize] &= !(1 << (idx % u64::BITS as usize)) } } } } #[allow(unused)] fn clear_end(&mut self) { *self.data.last_mut().unwrap() &= !0 >> ((u64::BITS as usize - self.length % u64::BITS as usize) % u64::BITS as usize); } #[allow(unused)] pub fn count_zeros(&self) -> usize { self.data.iter().map(|&d| d.count_zeros() as usize).sum() } #[allow(unused)] pub fn count_ones(&self) -> usize { self.data.iter().map(|&d| d.count_ones() as usize).sum() } #[allow(unused)] pub fn trailing_zeros(&self) -> usize { match self.data.iter().position(|&d| d != 0) { Some(border_pos) => self.length.min( u64::BITS as usize * border_pos + self.data[border_pos].trailing_zeros() as usize, ), None => self.length, } } #[allow(unused)] pub fn trailing_ones(&self) -> usize { match self.data.iter().position(|&d| d != !0) { Some(border_pos) => self.length.min( u64::BITS as usize * border_pos + self.data[border_pos].trailing_ones() as usize, ), None => self.length, } } #[allow(unused)] pub fn leading_zeros(&self) -> usize { let first_mask = !0 >> ((u64::BITS as usize - self.length % u64::BITS as usize) % u64::BITS as usize); let first_element = *self.data.last().unwrap() & first_mask; let first_lead = first_element.leading_zeros() as usize - ((u64::BITS as usize - self.length % u64::BITS as usize) % u64::BITS as usize); if first_element == 0 { match self.data.iter().rev().skip(1).position(|&d| d != 0) { Some(border_pos) => self.length.min( first_lead + u64::BITS as usize * border_pos + self.data[self.data.len() - 2 - border_pos].leading_zeros() as usize, ), None => self.length, } } else { first_lead } } #[allow(unused)] pub fn leading_ones(&self) -> usize { let first_mask = !0 >> ((u64::BITS as usize - self.length % u64::BITS as usize) % u64::BITS as usize); let first_element = *self.data.last().unwrap() | !first_mask; let first_lead = first_element.leading_ones() as usize - ((u64::BITS as usize - self.length % u64::BITS as usize) % u64::BITS as usize); if first_element == !0 { match self.data.iter().rev().skip(1).position(|&d| d != !0) { Some(border_pos) => { first_lead + u64::BITS as usize * border_pos + self.data[self.data.len() - 2 - border_pos].leading_ones() as usize } None => self.length, } } else { first_lead } } #[allow(unused)] fn get_word_shl(&self, idx: usize, shift_rhs: usize) -> u64 { self.data .get(idx.wrapping_sub(shift_rhs / u64::BITS as usize)) .unwrap_or(&0) << (shift_rhs % u64::BITS as usize) | self .data .get(idx.wrapping_sub(shift_rhs / u64::BITS as usize + 1)) .unwrap_or(&0) >> (u64::BITS as usize - shift_rhs % u64::BITS as usize) } #[allow(unused)] fn get_word_shr(&self, idx: usize, shift_rhs: usize) -> u64 { self.data .get(idx + shift_rhs / u64::BITS as usize) .unwrap_or(&0) >> (shift_rhs % u64::BITS as usize) | self .data .get(idx + shift_rhs / u64::BITS as usize + 1) .unwrap_or(&0) << (u64::BITS as usize - shift_rhs % u64::BITS as usize) } #[allow(unused)] pub fn bitand_assign_self_shl(&mut self, rhs: T) where usize: TryFrom, { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; (0..self.data.len()) .rev() .for_each(|i| self.data[i] &= self.get_word_shl(i, rhs)); } #[allow(unused)] pub fn bitand_assign_self_shr(&mut self, rhs: T) where usize: TryFrom, { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; self.clear_end(); (0..self.data.len()).for_each(|i| self.data[i] &= self.get_word_shr(i, rhs)); } #[allow(unused)] pub fn bitor_assign_self_shl(&mut self, rhs: T) where usize: TryFrom, { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; (0..self.data.len()) .rev() .for_each(|i| self.data[i] |= self.get_word_shl(i, rhs)); self.clear_end(); } #[allow(unused)] pub fn bitor_assign_self_shr(&mut self, rhs: T) where usize: TryFrom, { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; self.clear_end(); (0..self.data.len()).for_each(|i| self.data[i] |= self.get_word_shr(i, rhs)); } #[allow(unused)] pub fn bitxor_assign_self_shl(&mut self, rhs: T) where usize: TryFrom, { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; (0..self.data.len()) .rev() .for_each(|i| self.data[i] ^= self.get_word_shl(i, rhs)); self.clear_end(); } #[allow(unused)] pub fn bitxor_assign_self_shr(&mut self, rhs: T) where usize: TryFrom, { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; self.clear_end(); (0..self.data.len()).for_each(|i| self.data[i] ^= self.get_word_shr(i, rhs)); } } impl Clone for BitSet { fn clone(&self) -> Self { Self { length: self.length, data: self.data.clone(), } } } impl std::ops::BitAndAssign for BitSet { fn bitand_assign(&mut self, rhs: Self) { self.data .iter_mut() .zip(rhs.data.into_iter()) .for_each(|(s, r)| *s &= r); } } impl std::ops::BitAndAssign<&Self> for BitSet { fn bitand_assign(&mut self, rhs: &Self) { self.data .iter_mut() .zip(rhs.data.iter()) .for_each(|(s, &r)| *s &= r); } } impl std::ops::BitOrAssign for BitSet { fn bitor_assign(&mut self, rhs: Self) { self.data .iter_mut() .zip(rhs.data.into_iter()) .for_each(|(s, r)| *s |= r); self.clear_end(); } } impl std::ops::BitOrAssign<&Self> for BitSet { fn bitor_assign(&mut self, rhs: &Self) { self.data .iter_mut() .zip(rhs.data.iter()) .for_each(|(s, &r)| *s |= r); } } impl std::ops::BitXorAssign for BitSet { fn bitxor_assign(&mut self, rhs: Self) { self.data .iter_mut() .zip(rhs.data.into_iter()) .for_each(|(s, r)| *s ^= r); self.clear_end(); } } impl std::ops::BitXorAssign<&Self> for BitSet { fn bitxor_assign(&mut self, rhs: &Self) { self.data .iter_mut() .zip(rhs.data.iter()) .for_each(|(s, &r)| *s ^= r); self.clear_end(); } } impl std::ops::ShlAssign for BitSet where usize: TryFrom, { fn shl_assign(&mut self, rhs: T) { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; (0..self.data.len()) .rev() .for_each(|i| self.data[i] = self.get_word_shl(i, rhs)); self.clear_end(); } } impl std::ops::ShrAssign for BitSet where usize: TryFrom, { fn shr_assign(&mut self, rhs: T) { let rhs = unsafe { usize::try_from(rhs).unwrap_unchecked() }; *self.data.last_mut().unwrap() &= !0 >> ((u64::BITS as usize - self.length % u64::BITS as usize) % u64::BITS as usize); (0..self.data.len()).for_each(|i| self.data[i] = self.get_word_shr(i, rhs)); } } impl PartialEq for BitSet { fn eq(&self, other: &Self) -> bool { self.length == other.length && self.data == other.data } fn ne(&self, other: &Self) -> bool { self.length != other.length || self.data != other.data } } impl Eq for BitSet {} } #[macro_export] macro_rules! read { ($iter:ident -> ($v:ident : $t1:tt $([$($t2:tt)+] $({$($t3:tt)+})?)?)) => { let $v = read_value!($iter -> $t1 $([$($t2)+] $({$($t3)+})? )?); }; ($iter:ident -> ($v:ident : $t1:tt $([$($t2:tt)+] $({$($t3:tt)+})?)? , $($r:tt)*)) => { read!($iter -> ($v : $t1 $([$($t2)+] $({$($t3)+})?)?)); read!($iter -> ($($r)*)); }; } #[macro_export] macro_rules! read_line { ($iter:ident -> ($($r:tt)*)) => { let cur_line = $iter.next().unwrap().unwrap(); let mut cur_line = cur_line.split_ascii_whitespace(); read!(cur_line -> ($($r)*)) } } #[macro_export] macro_rules! read_value { ($source:ident -> ($($t1:tt $([$($t2:tt)+])?),+)) => { ( $(read_value!($source -> $t1 $([$($t2)+])?)),* ) }; ($source:ident -> [ $t1:tt $([$($t3:tt)+])? ; $len:expr ]) => { ::std::array::from_fn::<_, $len, _>(|_| read_value!($source -> $t1 $([$($t3)+])?)) }; ($source:ident -> $t1:tt[ $t2:tt $([$($t3:tt)+])? ; $len:expr ]) => { (0..($len)).map(|_| read_value!($source -> $t2 $([$($t3)+])?)).collect::<$t1<_>>() }; ($source:ident -> $t1:tt[ $t2:tt $([$($t3:tt)+])? ]) => { (0..(read_value!($source -> u32))).map(|_| read_value!($source -> $t2 $([$($t3)+])?)).collect::<$t1<_>>() }; ($source:ident -> $t1:tt[ ($($t2:tt),+) ; $len:expr ] { $($p1:pat => ($($pos:tt),*)),* }) => { (0..($len)).map(|_| { let mut v = ($($t2::default()),+); v.0 = my_parser::parse_without_checking(($source).next().unwrap()); match v.0 { $($p1 => { $(v.$pos = my_parser::parse_without_checking(($source).next().unwrap()));* }),* _ => unreachable!(), } v }).collect::<$t1<_>>() }; ($source:ident -> $t1:tt[ ($($t2:tt),+) ] { $($p1:pat => ($($pos:tt),*)),* }) => { read_value!($source -> $t1[ ($($t2),+) ; read_value!($source -> u32) ] { $($p1 => ($($pos),*)),* }) }; ($source:ident -> $t:ty) => { my_parser::parse_without_checking::<$t>(($source).next().unwrap()) }; } mod my_parser { #[allow(unused)] pub unsafe fn parse_without_checking(target: &str) -> F { unsafe { Parsable::from_str(target) } } pub trait Parsable { unsafe fn from_str(s: &str) -> Self; } impl Parsable for String { unsafe fn from_str(s: &str) -> Self { Self::from(s) } } impl Parsable for char { unsafe fn from_str(s: &str) -> Self { s.chars().next().unwrap() } } macro_rules! parse_float { ($s:ident) => {{ let mut iter = $s.bytes().peekable(); let sign = match iter.peek().unwrap() { b'-' => { iter.next(); -1.0 } b'+' => { iter.next(); 1.0 } _ => 1.0, }; let mut result = 0.0; while let Some(cur) = iter.next() { if cur == b'.' { break; } result = result * 10.0 + (cur - b'0') as Self; } let mut digit = 1.0; (result + iter .map(|cur| { digit *= 0.1; digit * (cur - b'0') as Self }) .sum::()) * sign }}; } impl Parsable for u8 { unsafe fn from_str(s: &str) -> Self { ((((s.bytes().fold(0, |acc, x| (acc << 8) | (x as u32)) & 0x0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) as Self } } impl Parsable for u16 { unsafe fn from_str(s: &str) -> Self { ((((((s.bytes().fold(0, |acc, x| (acc << 8) | (x as u64)) & 0x0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) as Self } } impl Parsable for u32 { unsafe fn from_str(s: &str) -> Self { ((((((((s.bytes().fold(0, |acc, x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as Self } } impl Parsable for u64 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u64; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; s.as_bytes().chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as Self }) } } impl Parsable for u128 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u128; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; s.as_bytes().chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as Self }) } } impl Parsable for i8 { unsafe fn from_str(s: &str) -> Self { ((((((s .bytes() .skip(match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }) .fold(0, |acc, x| (acc << 8) | (x as u32)) & 0x0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) as i32) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i16 { unsafe fn from_str(s: &str) -> Self { ((((((((s .bytes() .skip(match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }) .fold(0, |acc, x| (acc << 8) | (x as u64)) & 0x0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) as i64) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i32 { unsafe fn from_str(s: &str) -> Self { ((((((((((s .bytes() .skip(match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }) .fold(0, |acc, x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as i128) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i64 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u64; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; let skip = match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }; ((s.as_bytes()[skip..].chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as u64 }) as i64) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i128 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u128; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; let skip = match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }; ((s.as_bytes()[skip..].chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) }) as i128) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for f32 { unsafe fn from_str(s: &str) -> Self { parse_float!(s) } } impl Parsable for f64 { unsafe fn from_str(s: &str) -> Self { parse_float!(s) } } } #[macro_export] macro_rules! write { ($out:expr) => {{ use std::io::Write; std::io::stdout() .lock() .write_all(($out).as_bytes()) .unwrap(); }}; } #[macro_export] macro_rules! format_iter { ($i:expr, $sep:expr, ($($elem:ident),+) -> ($form:expr $(, $ex:expr)*)) => {{ let mut iter = $i; #[allow(unused_parens)] let ($($elem),+) = iter.next().unwrap(); #[allow(unused_parens)] iter.fold(std::format!($form $(, $ex)*), |mut acc, ($($elem),+)| { use std::fmt::Write; acc.push_str($sep); std::write!(&mut acc, $form $(, $ex)*).unwrap(); acc }) }} } #[macro_export] macro_rules! format_vec { ($v:expr, $sep:expr, ($($elem:ident),+) -> ($form:expr $(, $ex:expr)*)) => {{ if $v.is_empty() { String::new() } else { let mut iter = $v.into_iter(); #[allow(unused_parens)] let ($($elem),+) = iter.next().unwrap(); #[allow(unused_parens)] iter.fold(std::format!($form $(, $ex)*), |mut acc, ($($elem),+)| { use std::fmt::Write; acc.push_str($sep); std::write!(&mut acc, $form $(, $ex)*).unwrap(); acc }) } }} } #[macro_export] macro_rules! format_vec_vec { ($v:expr, $sep1:expr, $sep2:expr, ($($elem:ident),+) -> ($form:expr $(, $ex:expr)*)) => {{ if $v.is_empty() { String::new() } else { let mut iter = $v.into_iter(); let v_first = iter.next().unwrap(); #[allow(unused_parens)] v.into_iter().fold(format_vec!(v_first, $sep2, ($($elem),+) -> ($form $(, $ex)*)), |mut acc, mut v| { use std::fmt::Write; acc.push_str($sep1); if !v.is_empty() { let mut iter_inner = v.into_iter(); #[allow(unused_parens)] let ($($elem),+) = iter_inner.next().unwrap(); std::write!(&mut acc, $form $(, $ex)*).unwrap(); iter_inner.fold(acc, |mut acc, ($($elem),+)| { acc.push_str($sep2); std::write!(&mut acc, $form $(, $ex)*).unwrap(); acc }) } }) } }} }