T = gets.to_i T.times do x, y = gets.split.map(&:to_i) max_idx = 0 idx = y while x[idx] == 1 max_idx = idx idx += 1 end puts 2 ** idx - 1 end