Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Chef's good friend Shubham has an assignment to complete, but he is too lazy to do it, so he asked Chef to help him. On the other hand, Chef was busy in the kitchen, so he in turn asked you to help Shubham.

You are given a sequence of positive integers $A_{1}, A_{2}, \ldots, A_{N}$ and an integer $K$. For any positive integer $X$ such that exactly $K$ bits in the binary representation of $X$ are equal to $1$, consider the sum $S = \sum_{i=1}^N (X \wedge A_{i})$; here, $\wedge$ denotes bitwise AND. You should choose $X$ in such a way that $S$ is maximum possible. If there is more than one such value of $X$, you should find the smallest one.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $K$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.

------  Output ------
For each test case, print a single line containing one integer ― the smallest possible value of $X$.

------  Constraints ------
$1 ≤ T ≤ 1,000$
$1 ≤ N ≤ 10^{5}$
$1 ≤ K ≤ 30$
$1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{5}$

------  Subtasks ------
Subtask #1 (50 points): $K ≤ 2$

Subtask #2 (50 points): original constraints

----- Sample Input 1 ------ 
1

4 1

3 4 5 1
----- Sample Output 1 ------ 
4
----- explanation 1 ------ 
Example case 1: Exactly one bit in $X$ should be $1$. Our options are:
- If $X = 1$, $S = 1+0+1+1 = 3$.
- If $X = 2$, $S = 2+0+0+0 = 2$.
- If $X = 4$, $S = 0+4+4+0 = 8$.
- For any greater valid $X$, $S = 0$.

The maximum value of $S$ is $8$, so the answer is the only value of $X$ where we get $S = 8$.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Bit manipulation

Mathematics


Solutions:
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = [0] * 30
	for j in a:
		c = j
		r = 0
		while c:
			b[r] += c % 2
			c //= 2
			r += 1
	u = []
	s = 1
	for z in range(30):
		b[z] *= s
		u.append(b[z])
		s *= 2
	u.sort(reverse=True)
	d = []
	ans = 0
	for p in range(k):
		h = u[p]
		for r in range(30):
			if b[r] == h and r not in d:
				d.append(r)
				break
		ans += 2 ** r
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [0] * 30
	for i in a:
		temp = i
		j = 0
		while temp:
			b[j] += temp % 2
			temp = temp // 2
			j += 1
	c = []
	ss = 1
	for i in range(30):
		b[i] = b[i] * ss
		c.append(b[i])
		ss = ss * 2
	c.sort(reverse=True)
	d = []
	ans = 0
	for i in range(k):
		t = c[i]
		for j in range(30):
			if b[j] == t and j not in d:
				d.append(j)
				break
		ans += 2 ** j
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	count = [0] * 30
	ar = list(map(int, input().split()))
	for elm in ar:
		for i in range(30):
			if elm & 1 << i > 0:
				count[i] += 1
	val = []
	for i in range(30):
		val.append((-count[i] * (1 << i), i))
	val.sort()
	ans = 0
	for i in range(k):
		ans = ans | 1 << val[i][1]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = [int(k) for k in input().split()]
	bits = [0] * 30
	for i in range(n):
		for j in range(0, 30):
			if arr[i] & 1 << j:
				bits[j] += 1
	sm = [0] * 30
	for i in range(30):
		sm[i] = bits[i] * (1 << i)
	tup = []
	for i in range(30):
		tup.append([sm[i], i])
	tup = sorted(tup, key=lambda x: -x[0])
	ans = 0
	for i in range(k):
		ans += 1 << tup[i][1]
	print(ans)


from heapq import heappop, heappush, heapify

def method_1(arr, n, k):
	heap = []
	for i in range(30):
		temp = 0
		for j in range(n):
			temp = temp + (arr[j] & 2 ** i)
		heap.append((-1 * temp, i))
	heapify(heap)
	res = 0
	for i in range(k):
		res = res + 2 ** heappop(heap)[1]
	print(res)
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	method_1(arr, n, k)


import sys
import math

def fout(s, end='\n'):
	sys.stdout.write(str(s) + end)

def fin():
	return sys.stdin.readline().strip()
t = int(fin())
while t > 0:
	t -= 1
	(n, k) = map(int, fin().split())
	a = [bin(int(x))[2:] for x in fin().split()]
	le = len(max(a, key=lambda x: len(x)))
	temp = [0] * le
	for num in a:
		c = -1
		for i in range(len(num) - 1, -1, -1):
			if num[i] == '1':
				temp[c] += 1
			c -= 1
	for i in range(le):
		temp[i] = [temp[i] * 2 ** (le - i - 1), i]
	temp.sort(reverse=True)
	ans = ['0'] * le
	for i in range(min(le, k)):
		ans[temp[i][1]] = '1'
	k -= le
	if k > 0:
		ans = ['1'] * k + ans
	print(int(''.join(ans), 2))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = [0] * 30
	for i in range(n):
		b = bin(a[i]).replace('0b', '')
		b = b[::-1]
		for j in range(len(b)):
			c[j] += int(b[j])
	for i in range(30):
		c[i] = c[i] * 2 ** i
	d = [[i, c[i]] for i in range(30)]
	d = sorted(d, key=lambda x: (x[1], -x[0]))
	e = [0 for i in range(30)]
	for i in range(k):
		e[d[30 - i - 1][0]] = 1
	ans = ''.join((str(i) for i in e))
	ans = ans[::-1]
	print(int(ans, 2))


def insert(a):
	for i in range(1, len(a)):
		key = a[i].copy()
		j = i - 1
		while j >= 0 and a[j][1] >= key[1]:
			a[j + 1][0] = a[j][0]
			a[j + 1][1] = a[j][1]
			j -= 1
		a[j + 1][0] = key[0]
		a[j + 1][1] = key[1]
	return a
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = [0] * 30
	for i in range(n):
		b = bin(a[i]).replace('0b', '')
		b = b[::-1]
		for j in range(len(b)):
			c[j] += int(b[j])
	for i in range(30):
		c[i] = c[i] * 2 ** i
	d = [[i, c[i]] for i in range(30)]
	d = insert(d)
	e = [0 for i in range(30)]
	for i in range(k):
		e[d[30 - i - 1][0]] = 1
	ans = ''.join((str(i) for i in e))
	ans = ans[::-1]
	print(int(ans, 2))


for z in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	x = len(bin(max(a))[2:])
	y = []
	for i in a:
		t = len(bin(i)[2:])
		val = '0' * (x - t) + bin(i)[2:]
		y.append(val)
	s = []
	for i in range(x):
		st = ''
		for j in range(n):
			st += y[j][i]
		s.append(st)
	fin = []
	for i in range(x):
		p = [s[i].count('1') * 2 ** (x - i - 1), i]
		fin.append(p)
	fin.sort(reverse=True)
	res = [0 for i in range(x)]
	for i in range(min(x, k)):
		res[fin[i][1]] = 1
	ans = ''
	for i in res:
		ans += str(i)
	ans = '1' * (k - min(k, x)) + ans
	print(int(ans, 2))


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	s = []
	x = len(bin(max(l))[2:])
	for i in l:
		y = bin(i)[2:]
		y = (x - len(y)) * '0' + y
		s.append(y)
	y = []
	for i in range(x):
		z = ''
		for j in range(n):
			z += s[j][i]
		y.append(z)
	s = []
	for i in range(x):
		p = [y[i].count('1') * 2 ** (x - 1 - i), i]
		s.append(p)
	s.sort(reverse=True)
	res = [0 for i in range(x)]
	for i in range(min(k, x)):
		res[s[i][1]] = 1
	ans = ''
	for i in res:
		ans += str(i)
	ans = '1' * (k - min(k, x)) + ans
	print(int(ans, 2))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = [0] * 30
	c = [0] * 30
	power = 2 ** 29
	for i in range(30):
		c[i] = power
		power = power // 2
	for i in a:
		s = bin(i).replace('0b', '')
		l = len(s)
		j = -1
		while l > 0:
			if s[j] == '1':
				b[j] += 1
			j -= 1
			l -= 1
	d = [0] * 30
	for i in range(30):
		d[i] = b[i] * c[i]
	count = 0
	for i in range(k):
		m = max(d)
		l = len(d)
		j = -1
		while l > 0:
			if d[j] == m:
				count += c[j]
				del c[j]
				del d[j]
				break
			l -= 1
			j -= 1
	print(count)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(lambda x: bin(int(x))[2:], input().split()))
	arr = list(map(lambda x: '0' * (32 - len(str(x))) + str(x), arr))
	y = [0] * 32
	for i in range(32):
		for ele in arr:
			y[i] += int(ele[i])
	arr1 = []
	for i in range(32):
		ans = int(y[i]) * pow(2, 31 - i)
		arr1.append([31 - i, ans])
	gain = sorted(arr1, key=lambda x: x[1])[::-1]
	ans = 0
	for i in range(k):
		ans += pow(2, gain[i][0])
	print(ans)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	l1 = [0] * 32
	for t in l:
		for i in range(32):
			if t % 2 == 1:
				l1[i] += 2 ** i
			t = t // 2
			if t == 0:
				break
	l2 = l1[:]
	l2.sort()
	l3 = l2[32 - k:]
	ans = 0
	for i in l3:
		a = l1.index(i)
		ans += 2 ** a
		l1[a] = -1
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	temp = []
	for i in range(32):
		count = 0
		for j in range(n):
			if a[j] & 1 << i:
				count += 1
		temp.append([count * (1 << i), i])
	temp.sort(key=lambda x: x[0], reverse=True)
	res = 0
	for i in range(k):
		res += 1 << temp[i][1]
	print(res)


class SlotInfo:
	pos = 0
	count = 0
	unitValue = 0
	totalValue = 0

	def __repr__(self):
		return repr(self.__dict__)

def debug(*args, **kwargs):
	pass

def main():
	noOfTests = int(input())
	for i in range(noOfTests):
		print(runATest(i))

def runATest(_testIndex):
	[aCount, oneBitCount] = [int(x) for x in input().split()]
	if aCount == 0 or oneBitCount == 0:
		return 0
	aList = [int(x) for x in input().split()]
	maxA = max(aList)
	maxABinStr = '{0:0b}'.format(maxA)
	bitscape = len(maxABinStr)
	debug(maxA=maxA, maxABinStr=maxABinStr, bitscape=bitscape, oneBitCount=oneBitCount)
	if bitscape <= oneBitCount:
		return int('1' * oneBitCount, 2)
	slotInfoList = []
	for i in range(bitscape):
		slotInfo = SlotInfo()
		slotInfo.pos = i
		slotInfo.unitValue = 2 ** i
		slotInfoList.append(slotInfo)
	for i in aList:
		iStr = '{0:0b}'.format(i)
		for j in range(len(iStr)):
			value = iStr[-1 - j]
			if value == '1':
				slotInfo = slotInfoList[j]
				slotInfo.count += 1
	for slotInfo in slotInfoList:
		slotInfo.totalValue = slotInfo.unitValue * slotInfo.count + (99 - slotInfo.pos) / 100
	slotInfoList.sort(key=lambda x: x.totalValue, reverse=True)
	debug(slotInfoList)
	xList = ['0'] * bitscape
	for i in range(oneBitCount):
		slotInfo = slotInfoList[i]
		xList[bitscape - slotInfoList[i].pos - 1] = '1'
	debug(xList)
	output = int(''.join(xList), 2)
	return output
main()


for test in range(int(input())):
	bits = [0 for i in range(32)]
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for i in l:
		c = 0
		t = i
		while t:
			if t & 1:
				bits[c] += 1
			c += 1
			t >>= 1
	val = [bits[i] * 2 ** i for i in range(32)]
	dup = [bits[i] * 2 ** i for i in range(32)]
	dup.sort(reverse=True)
	b = [0 for i in range(32)]
	res = 0
	tem = []
	for i in dup[:k]:
		for j in range(32):
			if i == val[j] and b[j] == 0:
				b[j] = 1
				tem.append(j)
	for i in tem[:k]:
		res = res | 1 << i
	print(res)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(n, k) = iia()
	arr = iia()
	count = [0] * 32
	for i in arr:
		b = bin(i)[2:][::-1]
		for j in range(len(b)):
			if b[j] == '1':
				count[j] += 1
	vals = []
	for i in range(32):
		vals.append([2 ** i * count[i], i])
	vals.sort(key=lambda x: (-x[0], x[1]))
	res = 0
	for i in range(k):
		res += 2 ** vals[i][-1]
	print(res)


def to_bin(num, pad=None):
	bin_rep = []
	i = 0
	while True:
		if num < 2 ** i:
			break
		if num & 2 ** i:
			bin_rep.append(1)
		else:
			bin_rep.append(0)
		i += 1
	if pad:
		while len(bin_rep) < pad:
			bin_rep.append(0)
	return bin_rep
for _ in range(int(input())):
	(n, set_b) = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	req_len = len(to_bin(max(nums)))
	all_bin = [to_bin(i, req_len) for i in nums]
	if req_len < set_b:
		x = 0
		for i in range(set_b):
			x += 2 ** i
	else:
		count_pos = [[0, i] for i in range(req_len)]
		for i in range(req_len):
			for j in range(len(all_bin)):
				count_pos[i][0] += all_bin[j][i] * 2 ** i
		count_pos.sort(key=lambda x: (-x[0], x[1]))
		x = 0
		for i in range(set_b):
			x += 2 ** count_pos[i][1]
	print(x)


t = int(input())
for _ in range(t):
	(n, k) = list(map(lambda x: int(x), input().split()))
	a = list(map(lambda x: int(x), input().split()))
	values = {i: 0 for i in range(0, 32)}
	for number in a:
		for (i, bit) in enumerate(bin(number)[2:][::-1]):
			if bit == '1':
				values[i] += 1
	all_values = []
	for pos in values:
		all_values.append((pow(2, pos) * values[pos], pos))
	all_values.sort(key=lambda x: x[0], reverse=True)
	string = ['0'] * 32
	for value in all_values[0:k]:
		string[value[1]] = '1'
	print(int(''.join(string[::-1]), 2))


def binary(array, k):
	bits = [0 for i in range(32)]
	for i in range(len(array)):
		j = 31
		while array[i] != 0:
			a = array[i] % 2
			array[i] = array[i] // 2
			bits[j] += a
			j -= 1
	j = 0
	for i in range(len(bits) - 1, -1, -1):
		bits[i] = bits[i] * pow(2, j)
		j += 1
	for i in range(len(bits) // 2):
		(bits[i], bits[len(bits) - 1 - i]) = (bits[len(bits) - 1 - i], bits[i])
	bits = sorted(zip(bits, range(32)), key=lambda x: x[0], reverse=True)
	if k == 1:
		return 2 ** bits[0][1]
	elif k == 2:
		return 2 ** bits[0][1] + 2 ** bits[1][1]
	else:
		out = 0
		for i in range(k):
			out += 2 ** bits[i][1]
		return out
t = int(input())
for _ in range(t):
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	array = [int(temp) for temp in input().split()]
	print(binary(array, k))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	bits = [[] for i in range(35)]
	for i in range(n):
		temp = a[i]
		for j in range(35):
			bits[j].append(temp % 2)
			temp //= 2
	profit = []
	for i in range(35):
		profit.append(bits[i].count(1) * 2 ** i)
	x = [0 for i in range(35)]
	for i in range(k):
		(maximum, index) = (0, -1)
		for j in range(35):
			if profit[j] > maximum:
				index = j
				maximum = profit[j]
		if maximum > 0:
			x[index] = 1
			profit[index] = -1
		else:
			for j in range(35):
				if x[j] == 0:
					x[j] = 1
					break
	final_x = 0
	for i in range(34, -1, -1):
		final_x *= 2
		final_x += x[i]
	print(final_x)


def binary(x):
	l = []
	while x >= 1:
		rem = x % 2
		x = x // 2
		l.append(rem)
	return l
for i in range(int(input())):
	(a, k) = map(int, input().split())
	m = []
	l = []
	p = list(map(int, input().split()))
	for c in p:
		m.append(binary(c))
	count = 0
	for i in range(30):
		for j in range(len(m)):
			if len(m[j]) > i:
				if m[j][i] == 1:
					count += 1
		l.append([count * 2 ** i, i])
		count = 0
	l = sorted(l, key=lambda x: x[0], reverse=True)
	c = 0
	for i in range(k):
		c += 2 ** l[i][1]
	print(c)


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def func(a, b):
	if a[0] == b[0]:
		if a[1] < b[1]:
			return -1
		else:
			return 1
	if a[0] > b[0]:
		return -1
	else:
		return 1

def solve():
	for _ in range(ii()):
		(n, k) = mi()
		a = li()
		f = [0] * 32
		for i in a:
			for j in range(32):
				if i >> j & 1:
					f[j] += 1
		ans = 0
		x = [[(1 << i) * f[i], i] for i in range(32)]
		x.sort(key=ctk(func))
		ans = 0
		for i in range(k):
			ans |= 1 << x[i][1]
		print(ans)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	(n, k) = li()
	a = li()
	x = a[0]
	b = [0] * 30
	for i in range(n):
		p = bin(a[i])[2:].zfill(30)
		for j in range(30):
			b[j] += int(p[j])
	b = b[::-1]
	for i in range(30):
		b[i] = [b[i] * 2 ** i, -i]
	b.sort(reverse=True)
	c = [0] * 30
	for i in range(k):
		c[-b[i][1]] = 1
	c = ''.join(map(str, c))
	c = c[::-1]
	print(int(c, 2))
for _ in range(I()):
	solve()


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	(lola, mx, ans) = (0, 0, '')
	bits = [0 for i in range(31)]
	for i in range(n):
		(lp, start) = (arr[i], 0)
		while lp != 0:
			bits[start] += (lp & 1) * 2 ** start
			lp >>= 1
			mx = max(arr[i], mx)
			start += 1
	de = bin(mx).lstrip('0b')
	if len(de) <= k:
		ans = 2 ** k - 1
	else:
		(lo, ko, lim) = (k, [], 0)
		while lo > 0 and max(bits) > 0:
			ind = bits.index(max(bits))
			ko.append(ind)
			lo -= 1
			bits[ind] = 0
		while len(ko) < k and lim < 31:
			for i in range(31):
				if i not in ko:
					ko.append(i)
					break
			lim += 1
		for i in range(31):
			if i in ko:
				ans = '1' + ans
			else:
				ans = '0' + ans
		while ans.count('1') < k:
			ans = '1' + ans
		ans = int(ans, 2)
	print(ans)


t = int(input())
for _ in range(t):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	binary = []
	setbit = 0
	for i in range(32):
		count = 0
		for j in range(N):
			if A[j] % 2:
				count += 1
			A[j] = A[j] // 2
		if count:
			setbit += 1
		binary.append((count * 2 ** i, 2 ** i))
	binary = sorted(binary, key=lambda x: x[0], reverse=True)
	num = 0
	for i in range(K):
		num += binary[i][1]
	print(num)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	_map = {i: 0 for i in range(31)}
	for val in a:
		i = 0
		while val > 0:
			if val & 1:
				_map[i] += 1
			val = val >> 1
			i += 1
	arr = []
	for i in range(31):
		arr.append((_map[i] * (1 << i), i))
	arr.sort(reverse=True, key=lambda x: (x[0], -x[1]))
	ans = 0
	for i in range(k):
		ans = ans ^ 1 << arr[i][1]
	print(ans)


test = int(input())
for _ in range(test):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	count = [[0, i] for i in range(64)]
	for i in a:
		for j in range(64):
			count[-1 - j][0] += (i >> j & 1) * 2 ** j
	count.sort(reverse=True)
	num = ['0'] * 64
	for k in range(k):
		num[count[k][1]] = '1'
	print(int(''.join(num), 2))


for T in range(int(input())):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	count = [[0, i] for i in range(64)]
	for a in A:
		for i in range(64):
			count[-1 - i][0] += (a >> i & 1) * 2 ** i
	count.sort(reverse=True)
	num = ['0'] * 64
	for k in range(K):
		num[count[k][1]] = '1'
	print(int(''.join(num), 2))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	mask = 1
	t = [0] * 30
	for j in range(30):
		for (i, x) in enumerate(l):
			if x & mask << j:
				t[j] += 1
	c = 1
	for (i, x) in enumerate(t):
		t[i] *= c
		c *= 2
	t = sorted(zip(t, range(30)), key=lambda x: (-x[0], x[1]))
	ans = [0] * 30
	for i in range(k):
		x = t[i][1]
		ans[x] = 1
	ans = ans[::-1]
	s = ''
	for i in ans:
		s += str(i)
	print(int(s, 2))


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	nums = list(map(int, input().split()))
	binary = []
	maxlen = int(math.log(max(nums), 2)) + 1
	placeval = [0 for i in range(maxlen)]
	ans = [0 for i in range(maxlen)]
	for i in range(n):
		num = nums[i]
		num = bin(num)
		num = num[2:]
		num = '0' * (maxlen - len(num)) + num
		binary.append(num)
	for j in range(maxlen):
		for i in range(n):
			if binary[i][j] == '0':
				continue
			else:
				placeval[j] += 1
	for i in range(maxlen):
		placeval[i] = [placeval[i] * 2 ** (maxlen - i - 1), i]
	placeval.sort()
	for i in range(1, maxlen + 1):
		if k == 0:
			break
		ans[placeval[-i][1]] = 1
		k -= 1
	if k > 0:
		for i in range(1, maxlen + 1):
			if k == 0:
				break
			if ans[-i] == 0:
				ans[-i] = 1
				k -= 1
	ans = ''.join(map(str, ans))
	ans = '1' * k + ans
	print(int(ans, 2))


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	l = []
	for i in range(31):
		s = 0
		for j in range(n):
			if a[j] & 1 << i:
				s += 1
		l.append((s * (1 << i), 1 << i))
	l.sort(key=lambda x: (-x[0], x[1]))
	x = l[:k]
	ans = 0
	for i in range(k):
		ans += x[i][1]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	d = dict.fromkeys(range(30), 0)
	for i in range(n):
		x = bin(a[i]).replace('0b', '')
		p = len(x)
		for j in range(p):
			if x[p - j - 1] == '1':
				d[j] += pow(2, j)
	l = [0 for i in range(30)]
	m = list(d.values())
	m.sort(reverse=True)
	for i in range(k):
		for j in d:
			if d[j] == m[i]:
				l[j] = 1
				d[j] = -1
				break
	count = 0
	for i in range(len(l)):
		if l[i] == 1:
			count += pow(2, i)
	print(count)


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	maximum = max(a)
	maxLength = len(bin(maximum)) - 2
	if k >= maxLength:
		print(2 ** k - 1)
	else:
		oneCount = [0 for i in range(maxLength)]
		for num in a:
			reversedBinary = bin(num)[2:][::-1]
			for (i, bit) in enumerate(reversedBinary):
				oneCount[i] += int(bit)
		weights = []
		base = 0
		for count in oneCount:
			weights.append(count * 2 ** base)
			base += 1
		x = 0
		for i in range(k):
			index = weights.index(max(weights))
			x += 2 ** index
			weights[index] = -1
		print(x)


import sys

def finp():
	return sys.stdin.readline()

def fop(s):
	return sys.stdout.write(str(s) + '\n')

def fintinp():
	return int(sys.stdin.readline())

def flistintinp():
	return list(map(int, finp().split()))

def op(s):
	return s

def inp():
	return input()

def intop(s):
	return int(s)

def intinp():
	return int(input())

def listintinp():
	return list(map(int, inp().split()))

def listinp():
	return list(map(str, inp().split()))
import math
primelists = []
maxprecomprimeval = 100001
from math import ceil, sqrt
mysievelists = [i for i in range(maxprecomprimeval)]

def primeprecomputer():
	for i in range(2, math.ceil(math.sqrt(maxprecomprimeval))):
		if mysievelists[i] == i:
			for j in range(i * i, maxprecomprimeval, i):
				if mysievelists[j] == j:
					mysievelists[j] = i
	for i in range(2, maxprecomprimeval):
		if mysievelists[i] == i:
			primelists.append(mysievelists[i])
	return primelists
from bisect import bisect_right, bisect_left

def bisectright(lists, nbr):
	return bisect_right(lists, nbr)

def bisectleft(lists, nbr):
	return bisect_left(lists, nbr)
MOD = 100000007
mpflists = []

def mpf(n):
	k = 0
	primeprecomputer()
	while n != 1:
		print(mysievelists[n])
		mpflists.append(mysievelists[n])
		n //= mysievelists[n]
		k += 1
	return mpflists

def binary(n):
	l = []
	for i in range(31, -1, -1):
		if n >> i & 1 == 1:
			l.append(1)
		else:
			l.append(0)
	return l

def makebin(lists, n):
	p = [0] * 32
	for i in lists:
		m = binary(i)
		for j in range(32):
			p[j] += m[j]
	return p
for __ in range(intinp()):
	(n, k) = map(int, inp().split())
	lists = listintinp()
	l = makebin(lists, n)
	weightage = [0] * 32
	val = [0] * 32
	for w in range(32):
		ind = 32 - w - 1
		weight = 2 ** ind
		weightage[w] = l[w] * weight
		val[w] = weight
	v = [i[0] for i in sorted(enumerate(weightage), key=lambda x: x[1])]
	v.reverse()
	for i in range(32):
		v[i] = 32 - 1 - v[i]
	sums = 0
	for j in range(k):
		sums += 2 ** v[j]
	print(sums)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = [bin(int(x)).replace('0b', '') for x in input().split()]
	num = [0 for x in range(30)]
	for j in a:
		for m in range(len(j)):
			ti = len(j) - 1 - m
			if j[ti] == '1':
				num[m] += 1
	d = {}
	for j in range(30):
		num[j] *= pow(2, j)
		d[j] = num[j]
	sorte = sorted(d.items(), key=lambda x: x[1], reverse=True)
	total = 0
	for j in range(k):
		total += pow(2, sorte[j][0])
	print(total)


def decimalToBinary(n):
	return bin(n).replace('0b', '')
for _ in range(int(input())):
	(N, K) = map(int, input().split())
	array = list(map(int, input().split()))
	for j in range(0, len(array)):
		ans = decimalToBinary(array[j])
		array[j] = '0' * (30 - len(ans)) + ans
	lookup = {}
	for i in range(30):
		if i not in lookup:
			lookup[i] = 0
	for i in range(len(array)):
		for j in range(len(array[i])):
			if array[i][j] == '1':
				lookup[29 - j] += 1
	answer = list(lookup.values())
	for j in range(len(answer)):
		answer[j] = [answer[j] * 2 ** j, j]
	answer.sort(key=lambda x: x[0], reverse=True)
	ans = answer[:K]
	total = 0
	for i in range(len(ans)):
		total += 2 ** ans[i][1]
	print(total)


try:
	for _ in range(int(input())):
		(n, k) = list(map(int, input().split(' ')))
		A = list(map(int, input().split(' ')))
		contrOfEachPlc = [0] * 31
		for i in range(n):
			a = format(A[i], '031b')
			A[i] = a
		po = 30
		for i in range(31):
			for j in range(n):
				contrOfEachPlc[i] += int(A[j][i])
			contrOfEachPlc[i] = contrOfEachPlc[i] * pow(2, po)
			po -= 1
		reversingContrOfEachPlc = [0] * 31
		pos = 0
		d = {}
		for i in range(30, -1, -1):
			reversingContrOfEachPlc[pos] = contrOfEachPlc[i]
			d[pos] = reversingContrOfEachPlc[pos]
			pos += 1
		d = sorted(d.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
		x = 0
		lst = []
		i = 0
		while i < 30:
			if d[i][1] > 0:
				flag = 0
				srt = i
				while d[i][1] == d[i + 1][1]:
					flag = 1
					i += 1
				if flag == 1:
					for a in range(i, srt - 1, -1):
						lst.append(d[a])
					i += 1
				if flag == 0:
					lst.append(d[i])
					i += 1
			else:
				break
		for i in range(30, -1, -1):
			if d[i][1] == 0:
				lst.append(d[i])
		for i in range(k):
			x += pow(2, lst[i][0])
		print(x)
except:
	pass


from collections import Counter
from operator import itemgetter

def solve(A, K):
	vals = [0] * 30
	for a in A:
		for (i, bit) in enumerate(bin(a)[-1:1:-1]):
			if bit == '1':
				vals[i] += 1 << i
	to_sort = [(val, i) for (i, val) in enumerate(vals)]
	to_sort.sort(key=itemgetter(0), reverse=True)
	ans = 0
	for (_, i) in to_sort[:K]:
		ans += 1 << i
	return ans
for _ in range(int(input())):
	(N, K) = map(int, input().split())
	A = map(int, input().split())
	ans = solve(A, K)
	print(ans)


def Solve(k, arr):
	a = [0] * 30
	s = ''
	for i in range(len(arr)):
		s = bin(arr[i])[2:]
		j = 0
		for ones in range(len(s) - 1, -1, -1):
			if s[ones] == '1':
				a[j] += 1
			j += 1
	sums = [0] * 30
	kk = 0
	for i in a:
		sums[kk] = i * 2 ** kk
		kk += 1
	vals = sorted(zip(sums, range(len(sums))), key=lambda x: x[0], reverse=True)
	if k == 1:
		return 2 ** vals[0][1]
	elif k == 2:
		return 2 ** vals[0][1] + 2 ** vals[1][1]
	else:
		out = 0
		for i in range(k):
			out += 2 ** vals[i][1]
		return out
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(Solve(k, arr))


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	brep = []
	length = 30
	for ele in arr:
		brep.append('0' * (length - len(bin(ele)[2:])) + bin(ele)[2:])
	dic = dict()
	for i in range(0, length):
		count = 0
		for ele in brep:
			if ele[length - i - 1] == '1':
				count += 1
		dic[i] = count * pow(2, i)
	dic = sorted(dic.items(), key=lambda kv: (-kv[1], kv[0]))
	print(sum((pow(2, ele[0]) for ele in dic[:k])))
	t -= 1


t = int(input())
for i in range(t):
	a = list(input().split())
	n = int(a[0])
	kk = int(a[1])
	a = list(input().split())
	dic = {}
	for k in a:
		m = int(k)
		c = -1
		while m > 0:
			c += 1
			if m & 1 == 1:
				dic[c] = dic.get(c, 0) + pow(2, c)
			else:
				dic[c] = dic.get(c, 0)
			m = m >> 1
	dic_l = sorted(dic, key=lambda x: (-dic[x], x))
	co = 0
	res = 0
	for k in dic_l:
		res = res + pow(2, k)
		co = co + 1
		if co == kk:
			break
	while co != kk:
		res = res + pow(2, co)
		co = co + 1
	print(res)


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	cnt = [0] * 32
	for i in arr:
		for j in range(32):
			if i & 1 << j:
				cnt[j] += 1
	ans = 0
	cnt = [(cnt[i] * 2 ** i, i) for i in range(32)]
	cnt.sort(key=lambda x: x[0], reverse=True)
	cnt = cnt[0:k]
	for i in cnt:
		ans |= 1 << i[1]
	print(ans)


t = int(input())
while t:
	s = input().split()
	(n, k) = (int(s[0]), int(s[1]))
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	count = []
	for i in range(31):
		count.append(0)
	for i in a:
		bit = 0
		while i != 0:
			if i % 2 == 1:
				count[bit] += 1
			i = i // 2
			bit += 1
	contribution = dict()
	for i in range(31):
		contribution[i] = count[i] * 2 ** i
	contribution = sorted(contribution.items(), key=lambda x: x[1], reverse=True)
	x = 0
	for i in range(k):
		x += 2 ** contribution[i][0]
	print(x)
	t -= 1


import collections
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	List = list(map(int, input().split()))
	bits = collections.defaultdict(int)
	for each in List:
		s = bin(each)[:1:-1]
		for x in range(len(s)):
			if s[x] == '1':
				bits[x] += 1
	setbit = list(bits.keys())
	setbit.sort(reverse=True, key=lambda x: [bits[x] * 2 ** x, -x])
	ans = ['0'] * 33
	sett = 0
	for b in setbit[:k]:
		ans[b] = '1'
	if k > len(setbit):
		rem = k - len(setbit)
		i = 0
		while i < len(ans) and rem > 0:
			if ans[i] == '0':
				ans[i] = '1'
				rem -= 1
			i += 1
	print(int(''.join(ans[::-1]), 2))


try:
	t = int(input())
	for _ in range(t):
		(n, z) = map(int, input().split())
		l = [int(a) for a in input().split()]
		b = [bin(c)[2:] for c in l]
		m = max((len(i) for i in b))
		final_sum = []
		for j in range(m):
			k = sum((2 ** j & i for i in l))
			final_sum.append((-k, j))
		final_sum.sort()
		h = len(final_sum)
		for i in range(h, 32):
			final_sum.append((0, i))
		ans = 0
		for i in range(z):
			ans += 2 ** final_sum[i][1]
		print(ans)
except:
	pass


from collections import defaultdict as dd
l = []
i = 1
ct = 0
while ct <= 31:
	l.append(i)
	i *= 2
	ct += 1
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = [0] * 31
	h = k
	for i in range(0, 31):
		b = l[i]
		for j in a:
			if j & b == b:
				c[i] += 1
	d = dd(list)
	for i in range(0, 31):
		if l[i] == 0:
			continue
		b = l[i] * c[i]
		if b not in d:
			d[b] = [i]
		else:
			d[b] += [i]
	s = []
	ans = 0
	fg = 0
	vis = [0] * 31
	for i in sorted(d.keys(), reverse=True):
		for j in d[i]:
			k -= 1
			s.append(j)
			ans = ans | l[j]
			vis[j] = 1
			if k == 0:
				fg = 1
				break
		if fg == 1:
			break
	if k != 0:
		for i in range(0, 31):
			if vis[i] == 0:
				k -= 1
				ans = ans | l[i]
			if k == 0:
				break
	print(ans)


from copy import deepcopy

def solve(n):
	c = 0
	while n:
		if n & 1:
			b[c] += 1
		n >>= 1
		c += 1
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	b = [0] * 32
	for i in l:
		solve(i)
	for i in range(32):
		b[i] = pow(2, i) * b[i]
	el = deepcopy(b)
	el.sort(reverse=True)
	sum = 0
	for i in range(k):
		v = el[i]
		for j in range(31):
			if v == b[j]:
				b[j] = -1
				sum += pow(2, j)
				break
	print(sum)


import sys
from math import log2
read = sys.stdin.readline
for i in range(int(read())):
	(n, k) = list(map(int, read().split()))
	l = list(map(int, read().split()))
	d = dict()
	max_p = int(log2(max(l)))
	bit = 1
	for pos in range(max_p + 1):
		t = 0
		for e in l:
			t += e & bit
		d[pos] = t
		bit <<= 1
	if k > len(d):
		for i in range(max_p + 1, k + 1):
			d[i] = 0
	s = 0
	max_val = sorted(d, key=d.get, reverse=True)
	for i in range(k):
		s += 2 ** max_val[i]
	print(s)


for _ in range(int(input())):
	values = [[0, 2 ** i] for i in range(30)]
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	for a in arr:
		for value in values:
			value[0] += value[1] & a
	values.sort(key=lambda x: x[0], reverse=True)
	ans = 0
	for i in range(k):
		ans = ans | values[i][1]
	print(ans)


for _ in range(int(int(input()))):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	f = [0 for i in range(30)]
	for i in a:
		for j in range(30):
			f[j] += i >> j & 1
	vals = []
	for i in range(30):
		vals.append((f[i] * (1 << i), -i))
	vals.sort(reverse=True)
	res = 0
	for i in range(k):
		res |= 1 << -vals[i][1]
	print(res)


from functools import cmp_to_key

def maximum(a, b):
	if a[0] == b[0]:
		return a[1] - b[1]
	return b[0] - a[0]
t = int(input())
while t:
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	temp = []
	mul = 1
	for i in range(31):
		c = 0
		for j in range(n):
			if a[j] & 1 << i:
				c += 1
		temp.append([c * mul, i])
		mul *= 2
	temp = sorted(temp, key=cmp_to_key(maximum))
	ans = 0
	for i in range(k):
		ans += pow(2, temp[i][1])
	print(ans)
	t -= 1


from math import pow
import heapq

def power(x, n):
	if n == 0:
		return 1
	temp = power(x, int(n / 2))
	if n % 2 == 0:
		return temp * temp
	elif n > 0:
		return x * temp * temp
	else:
		return temp * temp / x
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	i = 31
	temp = [0 for i in range(32)]
	po = power(2, i)
	while i >= 0:
		for j in range(n):
			if arr[j] & po == po:
				temp[i] += 1
		temp[i] = temp[i] * po
		i -= 1
		po = po >> 1
	while k > 0:
		ele = max(temp)
		ind = temp.index(ele)
		temp[ind] = -1
		ans += power(2, ind)
		k -= 1
	print(ans)


t = int(input())
while t != 0:
	t = t - 1
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a1 = [0] * 30
	for i in a:
		s1 = bin(i)
		n1 = len(s1)
		for j in range(n1 - 1, 1, -1):
			a1[n1 - 1 - j] = a1[n1 - 1 - j] + int(s1[j])
	flag = 1
	for i in range(30):
		a1[i] = a1[i] * flag
		flag = flag * 2
	a2 = list()
	for i in range(30):
		a3 = list()
		a3.append(a1[i])
		a3.append(i)
		a2.append(a3)
	a2.sort(key=lambda ff: (-ff[0], ff[1]))
	sum1 = 0
	for i in range(k):
		sum1 = sum1 + pow(2, a2[i][1])
	print(sum1)


for _ in range(int(input())):
	(N, K) = [int(i) for i in input().split(' ')]
	A = [int(i) for i in input().split(' ')]
	H = max(A)
	sett = len(str(bin(H))) - 2
	tab = [0] * sett
	for i in range(N):
		for j in range(1, len(str(bin(A[i]))) - 1):
			if str(bin(A[i]))[-j] == '1':
				tab[j - 1] += 1
	for i in range(len(tab)):
		tab[i] *= 2 ** i
	D = []
	for i in tab:
		D += [i]
	Inex = []
	tab.sort()
	TruthTable = [True] * len(D)
	for i in range(1, K + 1):
		for j in range(len(D)):
			if TruthTable[j]:
				if D[j] == tab[-i]:
					TruthTable[j] = False
					Inex += [j]
					break
	final = 0
	if len(Inex) == K:
		for i in Inex:
			final += 2 ** i
	elif K > sett:
		for i in range(K):
			final += 2 ** i
	print(final)


for test in range(int(input())):
	(n, k) = map(int, input().split())
	a = [0] * 30
	for A in input().split():
		A = int(A)
		for i in range(30):
			if 1 << i & A:
				a[i] += 1 << i
	ans = 0
	for i in sorted(list(range(30)), key=lambda i: (-a[i], i))[:k]:
		ans |= 1 << i
	print(ans)


for t in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	pairs = []
	for b in range(30):
		mysum = 0
		for i in range(len(arr)):
			if arr[i] >> b & 1 == 1:
				mysum += 2 ** b
		pairs.append((mysum, 50 - b))
	pairs.sort(reverse=True)
	mynum = 0
	for i in range(k):
		mynum += 2 ** (50 - pairs[i][1])
	print(mynum)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return len(set(factors))

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def printp(p):
	for i in p:
		print(i)
MOD = 10 ** 9 + 7
T = 1
T = int(stdin.readline())
for _ in range(T):
	(n, k) = list(map(int, stdin.readline().split()))
	a = list(map(int, stdin.readline().split()))
	cnt = []
	for i in range(31):
		cnt.append([0, i])
	for i in range(n):
		b = bin(a[i])[2:]
		for i in range(1, len(b) + 1):
			cnt[i - 1][0] -= int(b[-i])
	for i in range(31):
		cnt[i][0] = cnt[i][0] * 2 ** i
	cnt.sort()
	x = ['0'] * 31
	for i in range(k):
		x[30 - cnt[i][1]] = '1'
	print(int(''.join(x), 2))


try:
	from operator import itemgetter
	t = int(input())
	for v in range(t):
		(n, k) = map(int, input().split())
		a = list(map(int, input().split()))
		arr = [0 for i in range(30)]
		ind = [i for i in range(30)]
		for i in range(n):
			x = a[i]
			j = 0
			while x > 0:
				if x % 2 != 0:
					arr[j] = arr[j] + pow(2, j)
				x = x // 2
				j += 1
		ans = 0
		res = [list(p) for p in zip(*sorted(zip(arr, ind), key=itemgetter(0), reverse=True))]
		for i in range(k):
			ans += pow(2, res[1][i])
		print(ans)
except:
	pass


import math
import os
import random
import re
import sys
import heapq
import random
t = int(input())
while t > 0:
	t -= 1
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	contri = [0] * 35
	for a in arr:
		curr = a
		loc = 0
		mask = 1
		while curr != 0:
			if curr & mask != 0:
				contri[loc] += 1
				curr = curr & ~mask
			mask *= 2
			loc += 1
	pow = 0
	for i in range(len(contri)):
		contri[i] *= 2 ** pow
		pow += 1
	res = 0
	contri2 = [(contri[i], len(contri) - i) for i in range(len(contri))]
	contri2 = sorted(contri2, reverse=True)
	for i in contri2[:k]:
		res += 2 ** (len(contri) - i[1])
	print(res)


def f(n):
	l = []
	while n > 0:
		l.append(n % 2)
		n = n // 2
	return l
t = int(input())
while t > 0:
	t = t - 1
	(n, k) = map(int, input().split())
	a = input()
	A = list(map(int, list(a.split())))
	y = 0
	d = [0] * 30
	for i in range(n):
		l = f(A[i])
		m = len(l)
		y = max(y, m)
		for j in range(m):
			if l[j] == 1:
				d[j] = d[j] + 1
	val = [(1 << i) * d[i] for i in range(30)]
	s = 0
	while k > 0:
		k = k - 1
		i = val.index(max(val))
		s = s + (1 << i)
		val[i] = -1
	print(s)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split(' '))
	arr = list(map(int, input().split(' ')))
	bit_set = [0] * 30
	for i in range(30):
		for j in arr:
			if j & 1 << i:
				bit_set[i] += 1
	sum_arr = []
	digit_arr = []
	for i in range(30):
		dig = 0 | 1 << i
		digit_arr.append(dig)
		sum_arr.append(dig * bit_set[i])
	sum_digit = list(zip(sum_arr, digit_arr))
	sum_digit.sort(key=lambda x: x[0], reverse=True)
	x = 0
	for i in range(k):
		x += sum_digit[i][1]
	print(x)


from operator import itemgetter
from itertools import islice
R = lambda : map(int, input().split())
(t,) = R()
for _ in range(t):
	(n, k) = R()
	a = zip(*(f'{x:031b}' for x in R()))
	a = (x.count('1') for x in a)
	a = sorted(((-x << i, 1 << i) for (i, x) in zip(range(30, -1, -1), a)))
	print(sum(map(itemgetter(1), islice(a, k))))


for t in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	dic = [0 for i in range(30)]
	for i in arr:
		temp = bin(i)[2:][::-1]
		for j in range(len(temp)):
			if temp[j] == '1':
				dic[j] += 1
	lis = []
	for (index, count) in enumerate(dic):
		lis.append((2 ** index * count, index))
	lis.sort(key=lambda x: x[0], reverse=True)
	ans = 0
	for i in range(k):
		ans += 2 ** lis[i][1]
	print(ans)


t = int(input())
while t > 0:
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	d = [0] * 35
	for i in range(n):
		c = a[i]
		i = 0
		while c > 0:
			e = c % 2
			z = e * pow(2, i)
			d[i] += z
			c = c // 2
			i += 1
	sum1 = 0
	for i in range(k):
		max1 = -1
		z = 0
		for j in range(30):
			if d[j] > max1:
				max1 = d[j]
				z = j
		sum1 += pow(2, z)
		d[z] = -1
	print(sum1)
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	high_bits = [0 for i in range(30)]
	for i in arr:
		bin_n = bin(i)[2:]
		bin_n = bin_n[::-1]
		for (index, bit) in enumerate(bin_n):
			if bit == '1':
				high_bits[index] += 1
	high_include = 0
	ans_arr = []
	for (pow, frq) in enumerate(high_bits):
		ans_arr.append([2 ** pow * frq, pow])
	ans_arr.sort(key=lambda x: x[0], reverse=True)
	ans = 0
	while high_include < k:
		ans += 2 ** ans_arr[high_include][-1]
		high_include += 1
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	set_bits = [0] * 30
	for i in arr:
		x = bin(i)[2:]
		x = x[::-1]
		for j in range(len(x)):
			if x[j] == '1':
				set_bits[j] += 1
	dct = {}
	for i in range(30):
		cal = set_bits[i] * 2 ** i
		set_bits[i] = [i, cal]
		if cal in dct:
			dct[cal].append(i)
		else:
			dct[cal] = [i]
	set_bits.sort(key=lambda y: y[1], reverse=True)
	ans = 0
	chc = list(range(30))
	l = 0
	for i in range(k):
		if set_bits[i][1] == 0:
			ans += 2 ** chc[l]
			l += 1
		else:
			mn = min(dct[set_bits[i][1]])
			ans += 2 ** mn
			chc.remove(mn)
			dct[set_bits[i][1]].remove(mn)
	print(ans)


import math
temp = {}

def myfunction(e):
	global temp
	return (1 << e) * temp[e]

def sol(arr, N, K):
	global temp
	temp = dict()
	max_index = int(math.log2(max(arr)))
	for x in range(0, max_index + 1):
		for y in arr:
			try:
				temp[x] += (1 << x & y) >> x
			except:
				temp[x] = (1 << x & y) >> x
	myl = list(temp.keys())
	myl.sort(key=myfunction, reverse=True)
	ans = 0
	if K <= max_index + 1:
		for _ in range(K):
			ans += 1 << myl[_]
	else:
		ans = int('1' * K, 2)
	return ans
for _ in range(int(input())):
	(N, K) = map(int, input().strip().split(' '))
	arr = list(map(int, input().strip().split(' ')))
	print(sol(arr, N, K))


from math import *
t = int(input())
for z in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	bits = [0] * 31
	bits[0] = 1
	d = {}
	for i in range(1, 31):
		bits[i] = 2 * bits[i - 1]
	for i in range(len(bits)):
		re = 0
		for j in range(n):
			if bits[i] & l[j]:
				re += bits[i]
		d[i] = re
	d = sorted(d.items(), key=lambda x: x[1], reverse=True)
	s = 0
	for i in range(k):
		s += 2 ** d[i][0]
	print(s)


t = int(input())

def isSet(elem, k):
	if elem & 1 << k:
		return 1
	return 0
for _ in range(t):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	gem = [0] * 30
	kii = [0] * 30
	for i in range(len(x)):
		for j in range(30):
			if isSet(x[i], j):
				gem[j] += 1
	for i in range(30):
		kii[i] = gem[i] * 2 ** i
	z = []
	for i in range(k):
		r = max(kii)
		indo = kii.index(r)
		z.append(indo)
		kii[indo] = -1
	ans = 0
	for ele in z:
		ans += 1 << ele
	print(ans)


t = int(input())
for _ in range(t):
	(n, b) = map(int, input().split())
	arr = list(map(int, input().split()))
	o = 0
	for i in range(n):
		o |= arr[i]
	m = {}
	for k in range(0, 30):
		m[k] = 0
	for i in range(n):
		s = bin(arr[i])[2:][::-1]
		for j in range(len(s)):
			if s[j] == '1':
				m[j] += 2 ** j
	a1 = sorted(zip(m.values(), m.keys()), key=lambda x: (x[0], -x[1]), reverse=True)
	a2 = a1[::-1]
	ans1 = 0
	for (v, p) in a1:
		if b:
			ans1 += 2 ** p
			b -= 1
	print(ans1)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])

def func(a, b):
	if a[0] > b[0]:
		return -1
	elif a[0] < b[0]:
		return 1
	elif a[1] < b[1]:
		return -1
	else:
		return 1
for _ in range(inp()):
	(n, k) = ip()
	x = ip()
	K = [0] * 30
	for i in x:
		t = bin(i)[-1:1:-1]
		for j in range(len(t)):
			K[j] += int(t[j])
	for i in range(30):
		K[i] *= 1 << i
	K = [[K[i], i] for i in range(30)]
	from functools import cmp_to_key as c2k
	ind = sorted(K, key=c2k(func))
	ind = [j for (i, j) in ind][:k]
	K = ['0'] * 30
	for i in ind:
		K[i] = '1'
	K = K[::-1]
	print(int(''.join(K), 2))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	m = max(a)
	s = bin(m)[2:]
	l = len(s)
	v = []
	for i in range(n):
		p = bin(a[i])[2:]
		v.append(p)
	z = [0] * l
	for i in range(n):
		for j in range(len(v[i]) - 1, -1, -1):
			if v[i][j] == '1':
				z[len(v[i]) - 1 - j] += 1
	v = []
	for i in range(l):
		v.append(2 ** i * z[i])
	ans = 0
	if k > l:
		for i in range(l, k):
			ans += 2 ** i
		for i in range(l):
			mi = v.index(max(v))
			ans += 2 ** mi
			v[mi] = -1
	else:
		for i in range(k):
			mi = v.index(max(v))
			ans += 2 ** mi
			v[mi] = -1
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for j in range(30):
		c = 0
		t = 1 << j
		for i in l:
			if i & t:
				c += 1
		d[j] = c * 2 ** j
	sd = sorted(d.items(), key=lambda x: x[1], reverse=True)
	ans = 0
	for i in range(k):
		ans += 2 ** sd[i][0]
	print(ans)


import sys
import math
import heapq
from collections import defaultdict, deque
input = sys.stdin.readline

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	(n, k) = rm()
	a = rl()
	c = [0] * 30
	d = ['0'] * 30
	b = []
	for i in a:
		bb = list(bin(i))
		bb = bb[2:]
		b.append(bb)
		bc = bb[::-1]
		for i in range(len(bc)):
			c[29 - i] += 1 if bc[i] == '1' else 0
	e = []
	for i in range(30):
		e.append([c[i] * 2 ** (29 - i), i])
	e.sort(reverse=True)
	for i in range(k):
		d[e[i][1]] = '1'
	print(int(''.join(d), 2))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	bins = []
	bit = [0] * 30
	ans = [0] * 30
	w = [i for i in range(29, -1, -1)]
	for i in range(n):
		b = bin(arr[i])[2:]
		bins.append('0' * (30 - len(b)) + b)
	for i in range(30):
		count = 0
		for j in range(n):
			if bins[j][i] == '1':
				count += 1
		bit[i] = 2 ** (29 - i) * count
	x = zip(bit, w)
	x = sorted(x, key=lambda x: (-x[0], x[1]))
	i = 0
	while k > 0:
		k -= 1
		ans[29 - x[i][1]] = 1
		i += 1
	ans = ''.join((str(i) for i in ans))
	print(int(ans, 2))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	final = []
	for i in range(31):
		final.append([0, -i, 0])
	for x in a:
		tmp = bin(x)[2:]
		tmp = tmp[::-1]
		for i in range(len(tmp)):
			if tmp[i] == '1':
				final[i][2] += 1
				final[i][0] += 1 << i
	final.sort(reverse=True)
	ans = 0
	for i in range(k):
		ans |= 1 << abs(final[i][1])
	print(ans)