function Main(input) {
  let t = input.split("\n")[0], n = 1, a = new Array(n), s = new Array(n), x = 0;
  
  for (let i = 0; i < t; i++) {
    n = input.split("\n")[i*3+1];
    a = input.split("\n")[i*3+2].split(" ");
    s = input.split("\n")[i*3+3].split("");

    let tmp = 1;

    for (let j = 0; j < n; j++) {
      if (x == a[j]) tmp = 0;
      
      if (s[j] == 0) {
        if (tmp == 0) x = tmp;
      } else {
        if (tmp == 1) x =tmp;
      }
      
      tmp = 1;
    }
    
    console.log(x);
  }
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));