def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 a = list(map(int, data[idx:idx+N])) idx += N Q = int(data[idx]) idx += 1 b_list = list(map(int, data[idx:idx+Q])) mask = (1 << 60) - 1 # Initialize current states current_states = {} current_states[(0, 0)] = 0 for ai in a: next_states = {} for (y, f), cnt in current_states.items(): # Apply operation 1 if f == 0: y_op1 = y | ai f_op1 = 0 else: y_op1 = y & (~ai) f_op1 = 1 key = (y_op1, f_op1) if key not in next_states or cnt < next_states[key]: next_states[key] = cnt # Apply operation 2 if f == 0: y_op2 = y | ai f_op2 = 1 else: y_op2 = y & (~ai) f_op2 = 0 new_cnt = cnt + 1 key = (y_op2, f_op2) if key not in next_states or new_cnt < next_states[key]: next_states[key] = new_cnt current_states = next_states if not current_states: break # Process queries results = [] for b in b_list: min_op2 = float('inf') # Check target1: (b, 0) if (b, 0) in current_states: min_op2 = min(min_op2, current_states[(b, 0)]) # Check target2: (mask ^ b, 1) target2 = mask ^ b if (target2, 1) in current_states: min_op2 = min(min_op2, current_states[(target2, 1)]) if min_op2 == float('inf'): results.append(-1) else: results.append(min_op2) print('\n'.join(map(str, results))) if __name__ == "__main__": main()