Problem:
Score : 400 points

Problem Statement

There are two persons, numbered 0 and 1, and a variable x whose initial value is 0.
The two persons now play a game.
The game is played in N rounds. The following should be done in the i-th round (1 \leq i \leq N):

Person S_i does one of the following:

Replace x with x \oplus A_i, where \oplus represents bitwise XOR.

Do nothing.

Person 0 aims to have x=0 at the end of the game, while Person 1 aims to have x \neq 0 at the end of the game.

Determine whether x becomes 0 at the end of the game when the two persons play optimally.

Solve T test cases for each input file.

Constraints

1 \leq T \leq 100

1 \leq N \leq 200

1 \leq A_i \leq 10^{18}

S is a string of length N consisting of 0 and 1.

All numbers in input are integers.

Input

Input is given from Standard Input in the following format.
The first line is as follows:

T

Then, T test cases follow.
Each test case is given in the following format:

N
A_1 A_2 \cdots A_N
S

Output

For each test case, print a line containing 0 if x becomes 0 at the end of the game, and 1 otherwise.

Sample Input 1

3
2
1 2
10
2
1 1
10
6
2 3 4 5 6 7
111000

Sample Output 1

1
0
0

In the first test case, if Person 1 replaces x with 0 \oplus 1=1, we surely have x \neq 0 at the end of the game, regardless of the choice of Person 0.

In the second test case, regardless of the choice of Person 1, Person 0 can make x=0 with a suitable choice.

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

use std::collections::BTreeSet;

const SIZE: u64 = 60;
fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let t: usize = sc.read();
    for _ in 0..t {
        let n: usize = sc.read();
        let a: Vec<u64> = sc.vec(n);
        let s = sc.chars().into_iter().map(|c| c == '0').collect::<Vec<_>>();
        if solve(a, s) {
            println!("0");
        } else {
            println!("1");
        }
    }
}

fn solve(mut a: Vec<u64>, mut s: Vec<bool>) -> bool {
    let n = a.len();
    if !s[n - 1] {
        return false;
    }

    let mut a0 = vec![];
    let mut a1 = vec![];

    let mut s0 = vec![];
    let mut s1 = vec![];

    let mut prev_turn = true;
    while let Some(turn) = s.pop() {
        if turn != prev_turn {
            if prev_turn {
                s0.push(a0);
                a0 = vec![];
            } else {
                s1.push(a1);
                a1 = vec![];
            }
            prev_turn = turn;
        }

        let a = a.pop().unwrap();
        if turn {
            a0.push(a);
        } else {
            a1.push(a);
        }
    }

    if !prev_turn {
        s1.push(a1);
    }

    assert_eq!(s0.len(), s1.len());
    let m = s0.len();
    let mut t0 = vec![];
    let mut t1 = vec![];
    for i in 0..m {
        t0.extend_from_slice(&s0[i]);
        t1.extend_from_slice(&s1[i]);

        // let mut t0 = s0[i].clone();
        // let mut t1 = s1[i].clone();

        let rank0 = normalize(&mut t0);
        let rank1 = normalize(&mut t1);

        for r1 in rank1.iter() {
            if !rank0.contains(&r1) {
                return false;
            }
        }
    }

    true
}

fn normalize(a: &mut Vec<u64>) -> Vec<u64> {
    let n = a.len();
    let mut cur = 0;
    let mut p = vec![];
    for pos in 0..SIZE {
        if let Some(i) = (cur..n).find(|&i| a[i] & (1 << pos) != 0) {
            a.swap(cur, i);
            for i in 0..n {
                if i == cur {
                    continue;
                }
                if a[i] & (1 << pos) != 0 {
                    a[i] ^= a[cur];
                }
            }
            cur += 1;
            p.push(pos);
        }
    }
    p
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}