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 Ruby (2.7.1) (judge verdict: Time Limit Exceeded)

require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i

def solve(index, x)
  return @memo[index][x] if @memo[index][x]
  if index == @n
    return @memo[index][x] ||= x == 0
  end
  return false unless ((~@or0[index]) & x) == 0
  # @cn += 1

  cur = @s[index]
  num = @a[index]
  if cur
    @memo[index][x] ||= solve(index+1, x) || solve(index+1, x ^ num)
  else
    @memo[index][x] ||= !(!solve(index+1, x) || !solve(index+1, x ^ num))
  end
end

t.times do |ti|
  @cn = 0

  @n = gets.chomp.to_i
  @a = gets.chomp.split.map(&:to_i)
  @s = gets.chomp.split(//).map{|c| c == '0'}

  bit_counts = Array.new(100, 0)
  @or0 = Array.new(@n, 0)
  @or1 = Array.new(@n, 0)
  if @s[-1]
    @or0[-1] = @a[-1]
    @a[-1].to_s(2).split(//).reverse.each_with_index do |c, i|
      bit_counts[i] += 1 if c == '1'
    end
  else
    @or1[-1] = @a[-1]
  end
  unless ((~@or0[-1]) & @or1[-1]) == 0
    puts '1'
    next
  end
  flg = false
  (@n-2).downto(0) do |i|
    if @s[i]
      @or0[i] = @or0[i+1] | @a[i]
      @or1[i] = @or1[i+1]
      @a[-1].to_s(2).split(//).reverse.each_with_index do |c, i|
        bit_counts[i] += 1 if c == '1'
      end
    else
      @or0[i] = @or0[i+1]
      @or1[i] = @or1[i+1] | @a[i]
      @a[-1].to_s(2).split(//).reverse.each_with_index do |c, i|
        bit_counts[i] -= 1 if c == '1'
      end
    end
    if (((~@or0[i]) & @or1[i]) != 0) || bit_counts.any?{|c| c < 0}
      flg = true
      break
    end
  end
  if flg
    puts '1'
    next
  end
  # p @or0.map{|e| e.to_s(2)}
  # p @or1.map{|e| e.to_s(2)}

  @memo = Array.new(@n+1){Hash.new}
  if solve(0, 0)
    puts '0'
  else
    puts '1'
  end
  # p ['count', @cn]
end