def solve_mini(N,A,Y,M=60): dp = [[1<<(M+1) for j in range(N+1)] for i in range(M+1)] for j in range(Y>>M,N+1,2): dp[M][j] = 0 low_sort = [[i for i in range(N)] for _ in range(M)] """ for t in range(M): low_sort[t].sort(key=lambda i:A[i]%(1<<(t+1)),reverse=True) """ pre = [i for i in range(N)] for t in range(M): zero,one = [],[] for i in pre: if A[i]>>t & 1: one.append(i) else: zero.append(i) pre = one + zero low_sort[t] = [i for i in pre] for i in range(M)[::-1]: zero,one,two = 0,0,0 for j in range(N): if A[j]>>i & 1: one += 1 else: zero += 1 if one&1==(Y>>i & 1): dp[i][0] = min(dp[i][0], 2*dp[i+1][two]) if (zero+two)&1==(Y>>i & 1): dp[i][0] = min(dp[i][0], 2*dp[i+1][one+two]+1) if i==0: break for j in range(1,N+1): next_up = low_sort[i-1][j-1] if A[next_up]>>i & 1: one -= 1 two += 1 else: zero -= 1 one += 1 if j!=N: next_next_up = low_sort[i-1][j] if A[next_up]%(1<<(i)) == A[next_next_up]%(1<<(i)): continue if one&1==(Y>>i & 1): dp[i][j] = min(dp[i][j], 2*dp[i+1][two]) if (zero+two)&1==(Y>>i & 1): dp[i][j] = min(dp[i][j], 2*dp[i+1][one+two]+1) if dp[0][0]==1<<(M+1): return -1 return dp[0][0] for _ in range(int(input())): N,Y = map(int,input().split()) A = list(map(int,input().split())) print(solve_mini(N,A,Y))