import sys def main(): N = int(sys.stdin.readline()) A_values = list(map(int, sys.stdin.readline().split())) Q_count = int(sys.stdin.readline()) B_queries = list(map(int, sys.stdin.readline().split())) ALL_ONES = (1 << 60) - 1 # dp stores: value -> min_op2_count # Initialize with x=0, 0 op2s used dp = {0: 0} for i in range(N): a_i = A_values[i] new_dp = {} # Iterate over states reachable in the previous step for x_prev, k_prev in dp.items(): # Operation 1: x = x_prev OR a_i x_op1 = x_prev | a_i k_op1 = k_prev # If x_op1 is new or we found a path with fewer op2s if x_op1 not in new_dp or k_op1 < new_dp[x_op1]: new_dp[x_op1] = k_op1 # Operation 2: x = NOT (x_prev OR a_i) intermediate_or_val = x_prev | a_i x_op2 = ALL_ONES ^ intermediate_or_val # Bitwise NOT over 60 bits k_op2 = k_prev + 1 # If x_op2 is new or we found a path with fewer op2s if x_op2 not in new_dp or k_op2 < new_dp[x_op2]: new_dp[x_op2] = k_op2 dp = new_dp # Update dp table for the current step results = [] for b_q in B_queries: results.append(str(dp.get(b_q, -1))) sys.stdout.write("\n".join(results) + "\n") if __name__ == '__main__': main()